[ACCEPTED]-TextField Validation With Regular Expression-ios4
I am not sure how you'd like to handle user 10 input and feedback. First I'll show a simple 9 way to keep the user in the editing mode 8 of the textField if her input is not valid.
First of all two delegate methods:
- (BOOL)textFieldShouldReturn:(UITextField *)aTextField
{
[aTextField resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)aTextField
{
return [self validateInputWithString:aTextField.text];
}
The testing method, which just returns YES or NO whether the input is valid or not:
- (BOOL)validateInputWithString:(NSString *)aString
{
NSString * const regularExpression = @"^([+-]{1})([0-9]{3})$";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regularExpression
options:NSRegularExpressionCaseInsensitive
error:&error];
if (error) {
NSLog(@"error %@", error);
}
NSUInteger numberOfMatches = [regex numberOfMatchesInString:aString
options:0
range:NSMakeRange(0, [aString length])];
return numberOfMatches > 0;
}
That's 7 it. However I'd recommend showing some live 6 status to the user whether his input is 5 ok or not. Add the following notifcation, for 4 example in your viewDidLoad method:
- (void)viewDidLoad
{
// ...
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(validateInputCallback:)
name:@"UITextFieldTextDidChangeNotification"
object:nil];
}
- (void)validateInputCallback:(id)sender
{
if ([self validateInputWithString:textField.text]) {
// For example turn a label green and let it say: "OK"
} else {
// For example turn a label red and let it say: "Allowed: + or minus followed by exactly three digits"
}
}
Finally: If you 3 need to access the capture groups (+ or 2 - and the number) of the regular expression 1 the following code will help:
// ... reg ex creation ...
NSArray *matches = [regex matchesInString:aString
options:0
range:NSMakeRange(0, [aString length])];
for (NSTextCheckingResult *match in matches) {
for (int i = 0; i < [match numberOfRanges]; i++) {
NSLog(@"range %d: %d %d", i, [match rangeAtIndex:i].location, [match rangeAtIndex:i].length);
NSLog(@"substring %d: %@", i, [aString substringWithRange:[match rangeAtIndex:i]]);
}
}
Validate Email id or Phone number using Regular Expression
Ddelegate methods:
- (BOOL)textFieldShouldReturn:(UITextField *)aTextField
{
[aTextField resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)aTextField
{
return [self validateEmail:aTextField.text]; // Change validateEmail to validatePhone for phone validation.
}
Returns YES or NO whether the input is valid or not:
- (BOOL) validateEmail: (NSString *) candidate {
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:candidate];
}
- (BOOL) validatePhone: (NSString *) candidate {
NSString *phoneRegex = @"^+(?:[0-9] ?){6,14}[0-9]$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
return [phoneTest evaluateWithObject:candidate];
}
0
Use UIPickerView instead
I am departing from the question title but 6 IMHO considering what you need, it may be 5 better to use UIPickerView to have a "spinning-wheel" type 4 of entry, like you do in the Clock app when 3 setting alarm. It can start at "+000" and 2 user can tumble some of the four wheels 1 ([+-]. [0-9], [0-9], [0-9]) as needed
Implement the -textField:shouldChangeCharactersInRange:replacementString
in your UITextFieldDelegate
and use NSRegularExpression
to validate 1 the changes.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.