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!
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.
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.
NSString *phoneRegex = @"^((\\+)|(00))[0-9]{6,14}$";
This way is bit better. Your code will work as well if you escape the "\".
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With