验证电话号码

发布于 2024-12-29 06:05:48 字数 659 浏览 7 评论 0原文

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

电话号码格式应该是国际格式,iPhone中是否有用于电话号码验证的正则表达式

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

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

这是我尝试过的代码

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

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

    if (isMatch == YES){

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

残花月 2025-01-05 06:05:48
    NSString * regex = @"((07|00447|004407|\\+4407|\\+447)\\d{9})";

既然找到了前导0或前导+44一次,为什么还要再寻找呢?

基本简化导致

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

then 到

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

then to,

    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...则为空),在 $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,然后从中删除所有非数字以进行进一步处理。

    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.

み格子的夏天 2025-01-05 06:05:48

^(\+?)(\d{13})$ 应该可以解决这个问题,转义斜线以便用于 Objective-C 的使用。
13 位数字,带有选项 + 前缀。

如果您想使用正则表达式,您可以使用这个之类的服务来获取视觉反馈,非常方便。

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

^(\+?)(\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})$";
-小熊_ 2025-01-05 06:05:48

这个怎么样?

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

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");
几味少女 2025-01-05 06:05:48
NSDataDetector *matchdetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber
                                                                error:&error];
NSUInteger matchNumber = [matchdetector numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])];

如果您使用 UITextField 那么:

textField.dataDetectorTypes = UIDataDetectorTypePhoneNumber;
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;
献世佛 2025-01-05 06:05:48

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

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

看看它是否对您有帮助

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文