I'm working on an application that will support both english and arabic languages. The application takes numeric input for use. In arabic language, the numeric characters are different! So, what's the best way to validate numeric input (amount, percentage, number) in such a situation?
A:
I ended up using NSNumberFormatter to check for the validity of the input, instead of using following methods:
+ (BOOL)validateNumeric:(NSString *)numericString {
NSString *regexExpression = @"^[-+]?\\d{0,9}$";
NSPredicate *matchTextPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexExpression];
return [matchTextPredicate evaluateWithObject:numericString];
}
OR
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
switch (fieldValidationType) {
case kValidationTypeAmount: {
NSCharacterSet *unacceptedInput = nil;
unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:CHARACTERS_AMOUNT] invertedSet];
if ([[self componentsSeparatedByCharactersInSet:unacceptedInput] count] > 1)
return NO;
else
return YES;
}
default:
return YES;
}
return YES;
}
Mustafa
2010-10-04 12:48:02