How would I go about allowing inputting only alphanumeric characters in an iOS UITextField
?
If you have a UITextField or UITextView and want to stop users typing in more than a certain number of letters, you need to set yourself as the delegate for the control then implement either shouldChangeCharactersIn (for text fields) or shouldChangeTextIn (for text views).
Alphanumeric, also referred to as alphameric, is a term that encompasses all of the letters and numerals in a given language set. In layouts designed for English language users, alphanumeric characters are those comprised of the combined set of the 26 alphabetic characters, A to Z, and the 10 Arabic numerals, 0 to 9.
Alphanumeric characters are the numbers 0-9 and letters A-Z (both uppercase and lowercase). An alphanumeric example are the characters a, H, 0, 5 and k. These characters are contrasted to non-alphanumeric ones, which are anything other than letters and numbers.
To use alphanumeric characters means to use both letters and numerals, and sometimes special characters, within a single string. Alphanumeric characters may be typed directly, in the case of passwords or filenames, or may be generated by a programmer using machine languages such as ASCII or Unicode.
Use the UITextFieldDelegate method -textField:shouldChangeCharactersInRange:replacementString:
with an NSCharacterSet containing the inverse of the characters you want to allow. For example:
// in -init, -initWithNibName:bundle:, or similar NSCharacterSet *blockedCharacters = [[[NSCharacterSet alphanumericCharacterSet] invertedSet] retain]; - (BOOL)textField:(UITextField *)field shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)characters { return ([characters rangeOfCharacterFromSet:blockedCharacters].location == NSNotFound); } // in -dealloc [blockedCharacters release];
Note that you’ll need to declare that your class implements the protocol (i.e. @interface MyClass : SomeSuperclass <UITextFieldDelegate>
) and set the text field’s delegate
to the instance of your class.
Swift 3 version
Currently accepted answer approach:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { // Get invalid characters let invalidChars = NSCharacterSet.alphanumerics.inverted // Attempt to find the range of invalid characters in the input string. This returns an optional. let range = string.rangeOfCharacter(from: invalidChars) if range != nil { // We have found an invalid character, don't allow the change return false } else { // No invalid character, allow the change return true } }
Another equally functional approach:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { // Get invalid characters let invalidChars = NSCharacterSet.alphanumerics.inverted // Make new string with invalid characters trimmed let newString = string.trimmingCharacters(in: invalidChars) if newString.characters.count < string.characters.count { // If there are less characters than we started with after trimming // this means there was an invalid character in the input. // Don't let the change go through return false } else { // Otherwise let the change go through return true } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With