7

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);
   }
}
Community
  • 1
  • 1
Rick
  • 1,153
  • 1
  • 18
  • 37
  • you really shouldn't limit yourself to this type of validation - just use a free JSON API like numverify.com – Frank Oct 03 '15 at 10:27

3 Answers3

26

EDIT: I read the question again and saw that you wanted to validate, not highlight the phone number, so I moved up the section on NSDataDetector.

You can use NSDataDetector (which is a specialized subclass of NSRegularExpression) to search for phone numbers in an NSString (this uses the iPhone's default phone number detection algorithm, I believe).

NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber
                                                           error:&error];
NSUInteger numberOfMatches = [detector numberOfMatchesInString:string
                                                       options:0
                                                         range:NSMakeRange(0, [string length])];

If you use a UITextView (but probably not UITextField like you are using here), you can highlight a phone number using the iPhone's default phone number detection algorithm by setting its dataDetectorTypes property:

textView.dataDetectorTypes = UIDataDetectorTypePhoneNumber;

See UIDataDetectorTypes for other kinds of data detectors.

Chaitanya Gupta
  • 4,043
  • 2
  • 31
  • 41
  • I tried NSDataDector and while I did not get it to work, I believe this would be the way to go. When I have more time to play with it I will change it to this format. Thanks – Rick Aug 24 '11 at 21:07
  • 1
    This is not a good solution because it will validate for invalid phone numbers if part of the phone number is valid. EX - This will return true for "(5555)555-5555" because "555-5555" is a valid phone number even though the entire string is not. – Josh at The Nerdery Oct 22 '12 at 15:50
4

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 NSRegularExpression that is a bit better for this than NSPredicate.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • OK I added this :Goals: this number is being entered via an alert. North American with or with out dashes. Thank you – Rick Aug 24 '11 at 19:14
  • 3
    Your regular expression looks nothing like this. There's not even a dash in it. Why do you believe it would work? Search http://regexlib.com for "NANP" to find numerous regexes for this. All have various trade-offs. I recommend being as forgiving as possible of anything a human hands you. – Rob Napier Aug 24 '11 at 19:19
  • @"^\\+?[0-9]*$" This one works. Funny The first one on regexlib.com does not in iOS but does in java android sdk. – Rick Aug 24 '11 at 21:10
  • Thank you for your help. This is a great community. I just hope I can put in as much help as I get from it. – Rick Aug 24 '11 at 21:10
0

Using this validation you can only enter 10 numbers. You can change it's length according to your requirement.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

 BOOL valid = [[NSPredicate predicateWithFormat:@"SELF MATCHES %@", REGEX_FOR_INTEGERS] evaluateWithObject:_textFieldValidate.text];



       if(valid){
           if(range.length + range.location > textField.text.length)
           {
               return NO;
           }

           NSUInteger newLength = [textField.text length] + [string length] - range.length;
           return newLength <= 10;


}


return YES;

}

Hope this will help you.