验证电话号码

时间:2012-01-25 09:38:09

标签: iphone ios4 regexkit

我想只有13个数值,或者13个数字值可以以“+”sysmbol.so为前缀,+不是强制性的 示例:1234567891234 另一个例子是:+1234567891234

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

我已经尝试了上面的链接,但是这个+1234545但是我想只有13个numarals或者+可以用13个数字作为前缀。

请让我知道,我可以在这里更改

这是我试过的代码

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

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

    if (isMatch == YES){

        NSLog(@"Matched");
    }
    else {
        NSLog(@"Not matched");
    }

6 个答案:

答案 0 :(得分:4)

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

找到前导0或前导+44一次,为什么要再次搜索它?

基本简化导致

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

然后到

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

然后到

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

但00不是唯一的常用拨号前缀,011在美国和加拿大使用。

添加并转动顺序,给出:

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

或者最好

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

开始时允许00447,011447,+ 447,004407,0114407,+ 4407,07,以及非捕获组。

对于更广泛的输入格式匹配,允许使用各种标点符号(连字符,括号,空格)

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

以1美元提取44个国家/地区代码(如果数字输入为07,则为null),以及2美元的10位NSN。

但是,请注意,从070和076开始的数字(除了07624)不是手机号码。

最终模式:

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

以2美元提取NSN,然后从中删除所有非数字以进行进一步处理。

答案 1 :(得分:1)

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

如果您使用UITextField,那么:

textField.dataDetectorTypes = UIDataDetectorTypePhoneNumber;

答案 2 :(得分:1)

这个怎么样?

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

答案 3 :(得分:1)

^(\+?)(\d{13})$应该做的伎俩,逃避斜线用于Objective-C的使用。 13位数字,带有选项+前缀。

如果您想使用正则表达式,可以使用this one之类的服务进行视觉反馈,非常方便。

NSString * forNumeric = @"^(\\+?)(\\d{13})$";

答案 4 :(得分:0)

答案 5 :(得分:0)

以下是我验证英国手机号码的方法:

- (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;
}

看看它是否对您有所帮助