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:
+
NSString *phoneRegex = @"^((\\+)|(00))[0-9]{6,14}$";
This way is bit better. Your code will work as well if you escape the "\".
well it depends on how strict you want to be it doesn't seem like this regex is especially strict. this regex says:
start at beginning of line match one + (or maybe 1 or 0) which seems ambiguous (but may not be depending on implementation) because the capture parentheses:() breaks up the relationship of the + and the ? possibly misplaced : match any digit 0-9 1 or 0 times 6-14 times then one digit 0-9 then end of line. also you note that any backslash will have to be doubled... @"\b" for a word boundary. you may want to try something like...
@"\\b[\\d]{3}\\-[\\d]{3}\\-[\\d]{4}\\b"
would I think match your example, but it wouldn't match
(555) 555 - 5555 or
555.555.5555 or
+44 1865 55555
Easy way to validate phone number and password limit , just follow the below process.
if ((self.mobileTxtFld.text.length < 10 )) {
[_mobileTxtFld becomeFirstResponder];
}
else if ((self.passwordTxtFld.text.length < kPasswordCharacterMinLimit) || (self.passwordTxtFld.text.length > kPasswordCharacterMaxLimit)) {
// show alert
}
after that you can implement textfiled delegate method "shouldChangeCharactersInRange" in that just write this code
if (textField == _mobileTxtFld) {
if([string rangeOfCharacterFromSet:ALLOWED_NUMBERS].location == NSNotFound){
NSUInteger newLength = [textField.text length] + [string length] - range.length;
if(newLength > kMobileNumberLimit - 1){
if(textField.text.length != kMobileNumberLimit){
textField.text = [NSString stringWithFormat:@"%@%@",textField.text,string];
}
[textField resignFirstResponder];
return NO;
}
else{
return YES;
}
}
else{
return NO;
}
}
return YES;
here ALLOWED_NUMBERS and kMobileNumberLimit are
#define kMobileNumberLimit 10
#define ALLOWED_NUMBERS [[NSCharacterSet decimalDigitCharacterSet] invertedSet]
#define minLimit 6
#define maxLimit 17
NSString *phoneRegex = @"^((\\+)|(00))[0-9]{6,14}|[0-9]{6,14}$";
This is tested RegularExpression This will accept with country code OR without country code
The only good way to validate phone numbers is to use Google's amazing LibPhoneNumber. There's an iOS port, or you can run the JavaScript implementation in a hidden UIWebView
.
(I've done the latter years ago when their was no iOS port yet. Works like a charm and is very fast even on old iPhones.)
-(int)findLength:(NSString*)phoneNumber
{
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
int length = [phoneNumber length];
return length;
}