I need to check to see if the user has input an alpha key or any symbol other than the decimal (.). I'm putting this inside an if statement, curious if there is an easy place to find this script. Any help would be greatly appreciated.
A:
Build up an NSCharacterSet
instance of your permitted characters, and test to see whether the string contains characters outside that set.
Graham Lee
2010-01-13 16:45:38
A:
You can add this method to your UITextFieldDelegate class, and this will be executed every time you enter text into the text field:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *set = [NSCharacterSet punctuationCharacterSet];
NSRange setRange = [string rangeOfCharacterFromSet:set];
if (setRange.location == NSNotFound)
{
textField.textColor = [UIColor redColor];
}
else
{
textField.textColor = [UIColor greenColor];
}
return YES;
}
Adrian Kosmaczewski
2010-01-13 16:49:35