validation电话号码ios
我需要validation一个国际电话号码。
我知道很难validation国际电话号码,所以我要保持简单:
+
或00然后6-14数字
我目前的代码使用正则expression式不工作,由于某些原因,我不能工作。 它只是说,它不能打开正则expression式和崩溃。
这是我现在的代码:
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}$";
这种方式好一点。 如果您转义“\”,您的代码也可以工作。
复制和粘贴方法来validation电话号码:
- (BOOL)validatePhone:(NSString *)phoneNumber { NSString *phoneRegex = @"^((\\+)|(00))[0-9]{6,14}$"; NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex]; return [phoneTest evaluateWithObject:phoneNumber]; }
或者用于validation电话号码的开源库
那么这取决于你想成为多严格,看起来不像这个正则expression式特别严格。 这个正则expression式说:
从行匹配的开头开始,一个+(或者可能是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是string(移动不,你会进入)
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; }
validation电话号码的唯一好方法是使用Google的惊人LibPhoneNumber 。 有一个iOS端口,或者你可以在隐藏的UIWebView
运行JavaScript实现。
(我已经做了后几年,当时他们还没有iOS的端口,作品像一个魅力,即使在旧的iPhone上也非常快。
简单的方法来validation电话号码和密码的限制,只需按照以下过程。
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
-(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;
}
- 使用正则expression式删除空行
- 在Swift中使用正则expression式进行简单search时,如何避免由于searchstring中的特殊字符造成的错误?
- Swiftreplace子string正则expression式
- 在为iPhone UITextViewinput(如Twitter)时build议#标签
- Objective-C中的正则expression式:如何用dynamic模板replace匹配?
- NSArray的NSArray中的对象的属性的NSPredicate
- 检查NSString是否包含任何数字,并且至less有7个字符长
- 如果正则expression式有效,如何检查Swift?
- 为什么正则expression式返回范围值?