验证电话号码
我只想有 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
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
既然找到了前导0或前导+44一次,为什么还要再寻找呢?
基本简化导致
then 到
then to,
但 00 并不是唯一常见的拨号前缀,011 在美国和加拿大使用。
添加这一点并翻转顺序,得到:
或最好
允许 00447、011447、+447、004407、0114407、+4407、07 在开头,并且带有非捕获组。
对于更广泛的输入格式匹配,允许使用各种标点符号(连字符、方括号、空格),
在 $1 中提取 44 个国家/地区代码(如果输入的数字为 07...则为空),在 $2 中提取 10 位 NSN。
但请注意,以 070 和 076 开头的号码(07624 除外)不是手机号码。
最终模式:
提取 $2 中的 NSN,然后从中删除所有非数字以进行进一步处理。
Having found the leading 0 or the leading +44 once, why search for it again?
Basic simplification leads to
then to
then to
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:
or preferably
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
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:
Extract the NSN in $2 then remove all non-digits from it for further processing.
^(\+?)(\d{13})$
应该可以解决这个问题,转义斜线以便用于 Objective-C 的使用。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.
这个怎么样?
How about this?
如果您使用
UITextField
那么:If you use
UITextField
then:您可以尝试使用 NSDataDetector:
http:// /developer.apple.com/library/ios/#documentation/Foundation/Reference/NSDataDetector_Class/Reference/Reference.html
适用于 iOS4+
you could try using a NSDataDetector:
http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSDataDetector_Class/Reference/Reference.html
available in iOS4+
以下是我验证英国手机号码的方法:
看看它是否对您有帮助
The following is what I do for validating UK mobile numbers:
See if it helps you