KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have two text fields that I would like to limit the number and type of characters. I have used the following bits of code to do each function separately but cannot find a way to do both within the same function. To restrict the type of character: - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { // Only characters in the NSCharacterSet you choose will insertable. NSCharacterSet *invalidCharSet = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefgABCDEFG"] invertedSet]; NSString *filtered = [[string componentsSeparatedByCharactersInSet:invalidCharSet] componentsJoinedByString:@""]; return [string isEqualToString:filtered]; } and to limit the number of characters: - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (textField.text.length >= 10 && range.length == 0) return NO; return YES; }
Tags (comma-separated)
Save Edits
Cancel