Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IPhone simple phone number validation

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);
   }
}
like image 737
Rick Avatar asked Aug 24 '11 19:08

Rick


People also ask

Is there a way to validate a phone number?

There are two ways to verify phone numbers, depending on what information you need: Phone verification API: An API service helps you validate phone numbers in real-time—think about when you are checking out online and you type your phone number into the field.

How do I validate a list of phone numbers?

No matter what industry you're in or what size your business is, it's useful to know how to validate phone numbers. The easiest way to verify phone numbers is to sign up for a phone validator. This tools reviews, standardizes, corrects, and removes hundreds (or even thousands) of numbers in seconds.

How do you check if a phone number is valid in Excel?

In the Data Validation dialog box, under the Settings tab, select Custom from the Allow drop down, and then enter this formula: =AND(ISNUMBER(A2),LEN(A2)=10) into the Formula text box, see screenshot: Note: In the above formula, A2 is the first cell of the column that you want to validate the phone number.


2 Answers

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.

like image 163
Chaitanya Gupta Avatar answered Sep 24 '22 01:09

Chaitanya Gupta


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.

like image 34
Rob Napier Avatar answered Sep 23 '22 01:09

Rob Napier