iPhone Developer Tips Visitor Stats: 126,691 Pageviews and 94,296 visitors in the past 30 days.
|
I’ve written a short character validation method that you can use as a starting point for validating characters against a character set. For devices running iPhone OS 3.x and thus support copy/paste, this code will also validate characters pasted into a textfield, and if invalid characters are found in the buffer, the input is not accepted.
The idea is to break apart the incoming string into substrings, using the invalid character set as the characters to split the string. The result returned is an array of objects that have been divided by the invalid characters. If the array has more than one entry, at least one invalid characters was found.
One note, typically the incoming string is just a single character (as typed by the user). However, for cases where the user has pasted a string, the string will vary in length.
If you are a fan of regular expressions you could change up the code using a library such as RegexKitLite.
To use this code, add the method below in the class which is the delegate for the TextField.
// You can add/tailor the acceptable values here...
#define CHARACTERS @" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
#define CHARACTERS_NUMBERS [CHARACTERS stringByAppendingString:@"1234567890"]
/*---------------------------------------------------
* Called whenever user enters/deletes character
*--------------------------------------------------*/
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
// These are the characters that are ~not~ acceptable
NSCharacterSet *unacceptedInput =
[[NSCharacterSet characterSetWithCharactersInString:CHARACTERS] invertedSet];
// Create array of strings from incoming string using the unacceptable
// characters as the trigger of where to split the string.
// If array has more than one entry, there was at least one unacceptable character
if ([[string componentsSeparatedByCharactersInSet:unacceptedInput] count] > 1)
return NO;
else
return YES;
}
You can adjust the character sets as you need to match the input requirements of your application. One more idea would be to manage the character sets inside the method, using a flag to indicate which set to compare against.
Share with iOS Developers:
Comments
2 Responses to “Validate User Input in UITextField, with Smarts to Properly Manage Copy and Paste”
Leave Comment
After this step: “The result returned is an array of objects that have been divided by the invalid characters.” — why not just check if ([array count > 1) ? If invalid characters are found, won’t there be 2 or more objects in the array?
Thanks Michelle, good catch, I’ve updated the code example based on your tip.