Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate phone number ios

I need to validate an international phone number.

I know its difficult to validate an international phone number, so I'm going to keep it simple:

+ or 00 then 6-14 numbers

My current code using a regex isn't working for some reason which I can't work out. It just says that it cannot open the regex and crashes.

Here is my current code:

NSString *phoneRegex = @"^[\+(00)][0-9]{6,14}$"; NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];  BOOL phoneValidates = [phoneTest evaluateWithObject:phoneNumber]; 

Where am I going wrong?

Thanks!

like image 266
Thomas Clayson Avatar asked Nov 01 '12 11:11

Thomas Clayson


People also ask

How do I validate a mobile number?

Mobile Number validation criteria:The first digit should contain numbers between 6 to 9. The rest 9 digit can contain any number between 0 to 9. The mobile number can have 11 digits also by including 0 at the starting. The mobile number can be of 12 digits also by including 91 at the starting.

What is the meaning of validate your mobile number?

Phone number validation is the process of checking if a phone number is accurate. It lets you find out if the phone number you have for a business contact or customer is active and able to receive calls.


2 Answers

NSString *phoneRegex = @"^((\\+)|(00))[0-9]{6,14}$"; 

This way is bit better. Your code will work as well if you escape the "\".

like image 166
whiteagle Avatar answered Oct 05 '22 02:10

whiteagle


Copy & Paste method to validate phone numbers:

- (BOOL)validatePhone:(NSString *)phoneNumber {     NSString *phoneRegex = @"^((\\+)|(00))[0-9]{6,14}$";     NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];      return [phoneTest evaluateWithObject:phoneNumber]; } 

or Open source Library for validating Phone Numbers

https://github.com/iziz/libPhoneNumber-iOS

like image 28
Manuel Escrig Avatar answered Oct 05 '22 03:10

Manuel Escrig