如何用正则表达式判断输入的手机号是否是规定的手机号
#pragma mark UITextFieldDelegate
//只允许输入数字,且只有11位数
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if (textField.text.length == 11) {
if ([string isEqualToString:@""]) {
return YES;
}
else {
return NO;
}
}
return [self validateNumber:string];
}
//只允许输入数字
- (BOOL)validateNumber:(NSString*)number{
BOOL res = YES;
NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
int i = 0;
while (i < number.length) {
NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
NSRange range = [string rangeOfCharacterFromSet:tmpSet];
if (range.length == 0) {
res = NO;
break;
}
i++;
}
return res;
}
//检查是否为手机号的方法
-(BOOL)checkPhoneNumInput:(NSString *)phoneStr
{
NSString *photoRange = @"^1(3[0-9]|4[0-9]|5[0-9]|7[0-9]|8[0-9])\\d{8}$";//正则表达式
NSPredicate *regexMobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",photoRange];
BOOL result = [regexMobile evaluateWithObject:phoneStr];
if (result) {
return YES;
} else {
return NO;
}
}