验证电话号码ios [英] Validate phone number ios

查看:112
本文介绍了验证电话号码ios的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要验证国际电话号码。

I need to validate an international phone number.

我知道很难验证国际电话号码,所以我会保持简单:

I know its difficult to validate an international phone number, so I'm going to keep it simple:

+ 或00然后6-14个数字

+ or 00 then 6-14 numbers

我的使用正则表达式的当前代码由于某些我无法解决的原因而无法正常工作。它只是说它无法打开正则表达式和崩溃。

My current code using a regex isn't working for some reason which I can't work out. It just says that it cannot open the regex and crashes.

这是我当前的代码:

NSString *phoneRegex = @"^[\+(00)][0-9]{6,14}$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];

BOOL phoneValidates = [phoneTest evaluateWithObject:phoneNumber];

我哪里出错?

谢谢!

推荐答案

NSString *phoneRegex = @"^((\\+)|(00))[0-9]{6,14}$";

这种方式更好一点。如果你逃避\,你的代码也会正常工作。

This way is bit better. Your code will work as well if you escape the "\".

这篇关于验证电话号码ios的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆