user198725878
user198725878

Reputation: 6386

Validating the phone numbers

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");
    }

Upvotes: 2

Views: 8644

Answers (6)

Deepesh
Deepesh

Reputation: 643

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;

Upvotes: 1

g1smd
g1smd

Reputation: 51

    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.

Upvotes: 4

Sveinung Kval Bakken
Sveinung Kval Bakken

Reputation: 3824

^(\+?)(\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})$";

Upvotes: 1

EmptyStack
EmptyStack

Reputation: 51374

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");

Upvotes: 1

Vin
Vin

Reputation: 10548

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

Upvotes: 0

Mike K
Mike K

Reputation: 2227

you could try using a NSDataDetector:

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

available in iOS4+

Upvotes: 0

Related Questions