Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restricting character input on iPhone UITextField [duplicate]

Possible Duplicate:
allow only alphanumeric characters for a UITextField

Is there a way of only letting the user input letters and numbers, no symbols, via the keyboard into a UITextField?

like image 318
daihovey Avatar asked Apr 03 '12 05:04

daihovey


2 Answers

Use this delegate methods.. as It works for me..

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(textField == YOUR TEXT FIELD)
    {
        NSCharacterSet *invalidCharSet = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:invalidCharSet] componentsJoinedByString:@""];
        return [string isEqualToString:filtered];
    }
    else
        return YES;
}
like image 64
Mehul Mistri Avatar answered Sep 30 '22 17:09

Mehul Mistri


- (BOOL) textField: (UITextField *)theTextField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string {    
  //return yes or no after comparing the characters
}
like image 33
Ankit Srivastava Avatar answered Sep 30 '22 16:09

Ankit Srivastava