Character Limit on iPhone UITextField

Possible duplicate:
allow only alphanumeric characters for UITextField

Is there a way to allow the user to enter letters and numbers, without characters, through the keyboard in a UITextField?

+6
source share
3 answers

Use this delegate method .. how does it work 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; } 
+9
source
 - (BOOL) textField: (UITextField *)theTextField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string { //return yes or no after comparing the characters } 
+2
source

Try the following:

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; if(![string stringByTrimmingCharactersInSet:nonNumberSet].length) { return NO; } return YES; 

}

+2
source

Source: https://habr.com/ru/post/912289/


All Articles