<!--
{
  "availability" : [
    "iOS: 2.0.0 - 27.0.0",
    "iPadOS: 2.0.0 - 27.0.0",
    "macCatalyst: 13.1.0 - 27.0.0",
    "tvOS: -",
    "visionOS: 1.0.0 - 27.0.0"
  ],
  "documentType" : "symbol",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/UITextFieldDelegate/textField(_:shouldChangeCharactersIn:replacementString:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:objc(pl)UITextFieldDelegate(im)textField:shouldChangeCharactersInRange:replacementString:"
  },
  "title" : "textField(_:shouldChangeCharactersIn:replacementString:)"
}
-->

# textField(_:shouldChangeCharactersIn:replacementString:)

Asks the delegate whether to change the specified text.

```
optional func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
```

## Parameters

`textField`

The text field containing the text.

`range`

The range of characters to be replaced.

`string`

The replacement string for the specified range. During typing, this parameter normally contains only the single new character that was typed, but it may contain more characters if the user is pasting text. When the user deletes one or more characters, the replacement string is empty.

## Return Value

<doc://com.apple.documentation/documentation/Swift/true> if the specified text range should be replaced; otherwise, <doc://com.apple.documentation/documentation/Swift/false> to keep the old text.

## Discussion

The text field calls this method whenever user actions cause its text to change. Use this method to validate text as it is typed by the user. For example, you could use this method to prevent the user from entering anything but numerical values.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)