通过输入框获取字符串后,我们判断电话号码是否合法
1.对NSString类进行扩展
2.写入方法,调用方便
@interface NSString (PhoneOrMail)
- (BOOL)isPhone;
- (BOOL)isMail;
- (BOOL)isOneOfPhoneAndMail;
@end
@implementation NSString (PhoneOrMail)
- (BOOL)isPhone{
//手机号以13, 15,18开头,八个 \d 数字字符
// NSString *phoneReg = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
//NSString *phoneReg = @"^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[06-8])\\d{8}$";
return [[NSPredicate predicateWithFormat:@"self matches %@",MOBILE] evaluateWithObject:self];
}
- (BOOL)isMail{
NSString *mailReg = @"^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
return [[NSPredicate predicateWithFormat:@"self matches %@",mailReg] evaluateWithObject:self];
}
- (BOOL)isOneOfPhoneAndMail{
if ([self rangeOfString:@"@"].location != NSNotFound) {
return [self isMail];
}else{
return [self isPhone];
}
}
@end