通過輸入框獲取字符串后,我們判斷電話號(hào)碼是否合法
1.對(duì)NSString類進(jìn)行擴(kuò)展
2.寫入方法,調(diào)用方便
@interface NSString (PhoneOrMail)
- (BOOL)isPhone;
- (BOOL)isMail;
- (BOOL)isOneOfPhoneAndMail;
@end
@implementation NSString (PhoneOrMail)
- (BOOL)isPhone{
//手機(jī)號(hào)以13, 15,18開頭,八個(gè) \d 數(shù)字字符
// 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