2

I want to have only 13 numeric values or the 13numeric values can be prefixed with "+" sysmbol.so the + is not mandatory Example : 1234567891234 another example is : +1234567891234

Telephone number format should be international,Is there any Regex for phone number validation in iPhone

I have tried the above link , but this +1234545 but i want to have only 13 numarals or + can be prefixed with that 13 numerals.

Please let me know , what can i change it here

This is the code i tried

NSString * forNumeric = @"^\\+(?:[0-9] ?){6,14}[0-9]$";

    BOOL isMatch = [[textFieldRounded text] isMatchedByRegex:forNumeric];

    if (isMatch == YES){

        NSLog(@"Matched");
    }
    else {
        NSLog(@"Not matched");
    }
Community
  • 1
  • 1
user198725878
  • 6,266
  • 18
  • 77
  • 135

6 Answers6

4
    NSString * regex = @"((07|00447|004407|\\+4407|\\+447)\\d{9})";

Having found the leading 0 or the leading +44 once, why search for it again?

Basic simplification leads to

    NSString * regex = @"((07|00440?7|\\+440?7)\\d{9})";

then to

    NSString * regex = @"((07|(00|\\+)440?7)\\d{9})";

then to

    NSString * regex = @"((0|(00|\\+)440?)7\\d{9})";

but 00 isn't the only common dial prefix, 011 is used in the US and Canada.

Adding that, and turning the order round, gives:

    NSString * regex = @"(^((0(0|11)|\\+)440?|0)7\\d{9}$)";

or preferably

    NSString * regex = @"(^(?:(?:0(?:0|11)|\\+)(44)0?|0)(7\\d{9}$))";

allowing 00447, 011447, +447, 004407, 0114407, +4407, 07 at the beginning, and with non-capturing groups.

For wider input format matching, allowing various punctuation (hyphens, brackets, spaces) use

    NSString * regex = @"(^\\(?(?:(?:0(?:0|11)\\)?[\\s-]?\\(?|\\+)(44)\\)?[\\s-]?\\(?(?:0\\)?[\\s-]?\\(?)?|0)(7\\d{9})$)";

Extract the 44 country code in $1 (null if number entered as 07...) and the 10-digit NSN in $2.

However, be aware that numbers beginning 070 and 076 (apart from 07624) are NOT mobile numbers.

The final pattern:

    NSString * regex = @"(^\\(?(?:(?:0(?:0|11)\\)?[\\s-]?\\(?|\\+)(44)\\)?[\\s-]?\\(?(?:0\\)?[\\s-]?\\(?)?|0)(7([1-5789]\\d{2}|624)\\)?[\\s-]?\\d{6}))$)";

Extract the NSN in $2 then remove all non-digits from it for further processing.

g1smd
  • 51
  • 3
1
NSDataDetector *matchdetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber
                                                                error:&error];
NSUInteger matchNumber = [matchdetector numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])];

If you use UITextField then:

textField.dataDetectorTypes = UIDataDetectorTypePhoneNumber;
Navnath Godse
  • 2,233
  • 2
  • 23
  • 32
Deepesh
  • 633
  • 4
  • 11
1

How about this?

NSString *forNumeric = @"\\+?[0-9]{6,13}";
NSPredicate *predicate;
predicate = [NSPredicate predicateWithFormat:@"self matches %@", forNumeric];
BOOL isMatch = [predicate evaluateWithObject:@"+1234567890123"];

if (isMatch) NSLog(@"Matched");
else NSLog(@"Not matched");
EmptyStack
  • 51,274
  • 23
  • 147
  • 178
1

^(\+?)(\d{13})$ should do the trick, escape the slashes for objective-C usage. 13 digits, with an options + prefix.

If you want to play with regexp expressions you can use services like this one for visual feedback, very handy.

NSString * forNumeric = @"^(\\+?)(\\d{13})$";
Sveinung Kval Bakken
  • 3,715
  • 1
  • 24
  • 30
  • 1
    In this case, The user has to enter a 13 digit number. What can be done if we want the user to enter anything less than 13 numbers? Like maybe 9-10 digits. – ScarletWitch Mar 27 '13 at 10:42
  • 3
    `NSString * forNumeric = @"^(\\+?)(\\d{a,b})$";` Just replace a and b with the inclusive range you want, e.g. a=9, b=10. – Sveinung Kval Bakken Mar 27 '13 at 19:20
0

you could try using a NSDataDetector:

http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSDataDetector_Class/Reference/Reference.html

available in iOS4+

Mike K
  • 2,227
  • 1
  • 12
  • 6
0

The following is what I do for validating UK mobile numbers:

- (BOOL) isValidPhoneNumber
{
    NSString * regex = @"((07|00447|004407|\\+4407|\\+447)\\d{9})";
    NSPredicate *testPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; 
    BOOL validationResult = [testPredicate evaluateWithObject: self];
    return validationResult;
}

See if it helps you

Vin
  • 10,517
  • 10
  • 58
  • 71