Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow only alphanumeric characters for a UITextField

How would I go about allowing inputting only alphanumeric characters in an iOS UITextField?

like image 602
WalterF Avatar asked Sep 24 '11 20:09

WalterF


People also ask

How do I limit the number of characters in 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).

What is only alphanumeric characters are allowed?

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.

What is an example of an alphanumeric character?

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.

How do you use alphanumeric characters?

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.


2 Answers

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.

like image 92
Noah Witherspoon Avatar answered Oct 12 '22 00:10

Noah Witherspoon


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     }  } 
like image 23
teradyl Avatar answered Oct 11 '22 22:10

teradyl