号码验证
// NSString *numStr = @"12345678901";
// NSLog(@"%@",[self isElevenNumber:numStr] ? @"是11位数字" : @"不是");
// NSString *email = @"123488555@qq.com";
// NSLog(@"%@",[self isEvaluateWithEmail:email] ? @"是" :@"不是");
NSString *number =@"11146299420";
NSLog(@"%@",[self isElevenNumber:number] ?@"是":@"不是");
NSLog(@"%@",[self isEvaluaWithPhoneNumber:number] ?@"是":@"不是");
}
- (BOOL)isEvaluaWithPhoneNumber:(NSString*)number {
NSString *MOBILE =@"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\\d{8}$";
NSString *CM =@"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$)|(^1705\\d{7}$)";
NSString *CU =@"(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}$)|(^1709\\d{7}$)";
NSString *CT =@"(^1(33|53|77|8[019])\\d{8}$)|(^1700\\d{7}$)";
if(![self isElevenNumber:number]) {
return NO;
}
BOOL isMobile = [self evaluateWithRegularExpression:MOBILE checkString:number];
BOOL isCM = [self evaluateWithRegularExpression:CM checkString:number];
BOOL isCU = [self evaluateWithRegularExpression:CU checkString:number];
BOOL isCT = [self evaluateWithRegularExpression:CT checkString:number];
if(isMobile) {
if(isCM) {
NSLog(@"中国移动");
}elseif(isCU) {
NSLog(@"中国联通");
}elseif(isCT){
NSLog(@"中国电信");
}
return YES;
}else{
NSLog(@"未知服务商");
return NO;
}
}
//验证邮箱
- (BOOL)isEvaluateWithEmail:(NSString *)email {
NSString *regular =@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{1,5}";
return[self evaluateWithRegularExpression:regular checkString:email];
}
//是否符合正则
- (BOOL)evaluateWithRegularExpression:(NSString *)regular checkString:(NSString *)checkStr {
NSPredicate *regularPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regular];
BOOL isYes = [regularPre evaluateWithObject:checkStr];
returnis Yes;
}
//是否是11位数字
- (BOOL)isElevenNumber:(NSString *)numStr {
//[0-9]{11,11}
NSString *regularStr =@"[0-9]{11,11}";
//MATCHES符合正则表达式
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regularStr];
//是否符合正则
BOOL isYes = [predicate evaluateWithObject:numStr];
return isYes;
}