I find all kinds of phone number validations on stackoverflow for the iphone but none seem to validate they just seem to change the format. example Phone number formatting.
Goals: this number is being entered via an alert. North American with or with out dashes.
so have some code but every thing is coming up as invalid any ideas.
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSString *str=@"^\\+(?:[0-9] ?){6,14}[0-9]$";
NSPredicate *no=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",str];
if([no evaluateWithObject:textField.text]==NO)
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Warning" message:@"Please Enter correct contact no." delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
else{
NSString *textValue = textField.text;
NSLog(@"Value: %@", textValue);
}
}
First, real phone number validation is pretty complicated. Are you limiting yourself to NANP (North America Numbering Plan), or are you looking for E.164 numbers? It looks like you’re trying to mostly match E.164, though this isn’t usually the kind of number that most people would enter.
Your match requires a plus, followed by a sequence of digits between seven and fifteen long with optional spaces between them. Is this what you mean? What strings are you passing in?
Note that as of iOS4, there is
NSRegularExpressionthat is a bit better for this thanNSPredicate.