xcode 用于手机号码验证的 NSPredicate
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15383130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSPredicate for mobile number validation
提问by Harshit K
How to validate a phone number (NSString *) by NSPredicate?
如何通过 NSPredicate 验证电话号码 (NSString *)?
Rules:
规则:
minimum 10 digits
最少 10 位数字
maximum 10 digits
最多 10 位
the first digit must be 7,8 or 9 Thanks
第一个数字必须是 7,8 或 9 谢谢
回答by Monolo
An NSPredicate
based on a regular expression will fit your requirements.
一种NSPredicate
基于正则表达式将满足您的要求。
NSString *stringToBeTested = @"8123456789";
NSString *mobileNumberPattern = @"[789][0-9]{9}";
NSPredicate *mobileNumberPred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", mobileNumberPattern];
BOOL matched = [mobileNumberPred evaluateWithObject:stringToBeTested];
You don't need to keep the pattern in a string by itself, but regexes are complicated enough already so it makes the overall code clearer if you keep it out of the NSPredicate
format string.
您不需要将模式单独保留在字符串中,但正则表达式已经足够复杂,因此如果将其保留在NSPredicate
格式字符串之外,它会使整个代码更清晰。
回答by Kirtikumar A.
You can just use below code
你可以只使用下面的代码
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
{
if(textField.tag == 111)
{
if([self MobileNumberValidate:string] == TRUE)
return YES;
else
return NO;
}
return YES;
}
#pragma mark - Mobile Number validation
- (BOOL)MobileNumberValidate:(NSString*)number
{
NSString *numberRegEx = @"[0-9]";
NSPredicate *numberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", numberRegEx];
if ([numberTest evaluateWithObject:number] == YES)
return TRUE;
else
return FALSE;
}
回答by Raj Aryan
Below code will work for your requirement:
下面的代码将适用于您的要求:
Function:
功能:
-(BOOL)validatePhone:(NSString *)enteredPhoneNumber
{
NSString *phoneRegex = @"[789][0-9]{9}";
// OR below for advanced type
//NSString *phoneRegex = @"^((\+)|(00))[0-9]{6,14}$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
return [phoneTest evaluateWithObject:enteredPhoneNumber];
}
Call it:
称它为:
if ([self validatePhone:@"9833112299"])
{
NSLog(@"Valid Phone Number");
}
else
{
NSLog(@"Invalid Phone Number");
}
回答by vilas deshmukh
Make it Global using 'extension' use it wherever required
使用“扩展”使其成为全球性的,在需要的地方使用它
In any one of your view controller class at the end after last }paste below code
在最后的任何一个视图控制器类中最后}粘贴下面的代码
extension String
{
func validateMobile() -> Bool
{
return NSPredicate(format: "SELF MATCHES %@","[789][0-9].{9}").evaluate(with: self)
}
}
when you want to validate yourTxtField in any ViewController class simply call as below:
当你想在任何 ViewController 类中验证 yourTxtField 时,只需调用如下:
if (yourTxtField.text?.validateMobile())!
{
print("It is 10 digit, starting with 7/8/9")
}
else
{
print("Invalid mobile number")
}
回答by 08442
NSString *phoneNumber = @"1234567890";
NSString *phoneRegex = @"[789][0-9]{3}([0-9]{6})?";
NSPredicate *test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
BOOL matches = [test evaluateWithObject:phoneNumber];