[英]Validate phone number ios
我需要验证一个国际电话号码。
我知道很难验证国际电话号码,所以我会保持简单:
+
或00然后是6-14个数字
我使用正则表达式的当前代码由于某些我无法解决的原因而无法正常工作。 它只是说它无法打开正则表达式和崩溃。
这是我目前的代码:
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}$";
这种方式有点好。 如果你逃避“\\”,你的代码也会工作。
复制和粘贴方法以验证电话号码:
- (BOOL)validatePhone:(NSString *)phoneNumber
{
NSString *phoneRegex = @"^((\\+)|(00))[0-9]{6,14}$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
return [phoneTest evaluateWithObject:phoneNumber];
}
或用于验证电话号码的开源库
这取决于你想要的严格程度,看起来这个正则表达式并不是特别严格。 这个正则表达式说:
从行匹配开始一个+(或者可能是1或0)看起来很模糊(但可能不依赖于实现),因为捕获括号:()打破了+和?的关系? 可能放错地方:匹配任何数字0-9 1或0次6-14次然后一位数0-9然后结束。 还要注意,任何反斜杠都必须加倍... @“\\ b”表示单词边界。 你可能想试试像...
@"\\b[\\d]{3}\\-[\\d]{3}\\-[\\d]{4}\\b"
would I think match your example, but it wouldn't match
(555) 555 - 5555 or
555.555.5555 or
+44 1865 55555
txtlpmobile.text是字符串(手机没有输入)
int length = [self getLength:txtLpMobile.text];
if(length == 10) {
if(range.length == 0)
return NO;
}
if(length == 3){
NSString *num = [self formatNumber:txtLpMobile.text];
txtLpMobile.text = [NSString stringWithFormat:@"(%@) ",num];
if(range.length > 0) {
txtLpMobile.text = [NSString stringWithFormat:@"%@",[num substringToIndex:3]];
}
} else if(length == 6) {
NSString *num = [self formatNumber:txtLpMobile.text];
txtLpMobile.text = [NSString stringWithFormat:@"(%@) %@-",[num substringToIndex:3],[num substringFromIndex:3]];
if(range.length > 0) {
txtLpMobile.text = [NSString stringWithFormat:@"(%@) %@",[num substringToIndex:3],[num substringFromIndex:3]];
}
}
NSUInteger newLength;
newLength = [txtLpMobile.text length] + [string length] - range.length;
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS_ONLY] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
return (([string isEqualToString:filtered])&&(newLength <= CHARACTER_LIMIT));
用于格式化数字
-(NSString*)formatNumber:(NSString*)mobileNumber
{
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
int length = [mobileNumber length];
if(length > 10)
{
mobileNumber = [mobileNumber substringFromIndex: length-10];
}
return mobileNumber;
}
获得长度
-(int)getLength:(NSString*)mobileNumber
{
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
int length = [mobileNumber length];
return length;
}
验证电话号码的唯一好方法是使用Google惊人的LibPhoneNumber 。 有一个iOS端口,或者您可以在隐藏的UIWebView
运行JavaScript实现。
(我已经完成了几年前,当时他们还没有iOS端口。就像魅力一样,即使在旧的iPhone上也很快。)
验证电话号码和密码限制的简便方法,请按照以下流程进行操作。
if ((self.mobileTxtFld.text.length < 10 )) {
[_mobileTxtFld becomeFirstResponder];
}
else if ((self.passwordTxtFld.text.length < kPasswordCharacterMinLimit) || (self.passwordTxtFld.text.length > kPasswordCharacterMaxLimit)) {
// show alert
}
之后你可以实现textfiled委托方法“shouldChangeCharactersInRange”,只需编写这段代码
if (textField == _mobileTxtFld) {
if([string rangeOfCharacterFromSet:ALLOWED_NUMBERS].location == NSNotFound){
NSUInteger newLength = [textField.text length] + [string length] - range.length;
if(newLength > kMobileNumberLimit - 1){
if(textField.text.length != kMobileNumberLimit){
textField.text = [NSString stringWithFormat:@"%@%@",textField.text,string];
}
[textField resignFirstResponder];
return NO;
}
else{
return YES;
}
}
else{
return NO;
}
}
return YES;
这里有ALLOWED_NUMBERS和kMobileNumberLimit
#define kMobileNumberLimit 10
#define ALLOWED_NUMBERS [[NSCharacterSet decimalDigitCharacterSet] invertedSet]
#define minLimit 6
#define maxLimit 17
在快速4 -
func ValidateMobileNumber(txtFeid : UITextField, strChk : String, range: NSRange) -> Bool {
if txtFeid.text!.count >= 10 {
return false
}
let formatePre = "^((\\+)|(00))[0-9]{6,14}$"
let resultPredicate : NSPredicate = NSPredicate(format: "SELF MATCHES %@",formatePre)
return resultPredicate.evaluate(with: strChk)
}
NSString *phoneRegex = @"^((\\\\+)|(00))[0-9]{6,14}|[0-9]{6,14}$";
这是经过测试的RegularExpression这将接受国家代码或没有国家代码
-(int)findLength:(NSString*)phoneNumber
{
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
int length = [phoneNumber length];
return length;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.