标签:
iOS - 正则表达式判断邮箱、身份证、手机号等是否正确
1 //邮箱 2 + (BOOL) validateEmail:(NSString *)email 3 { 4 NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; 5 NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; 6 return [emailTest evaluateWithObject:email]; 7 } 8 9 //手机号码验证 10 + (BOOL) validateMobile:(NSString *)mobile 11 { 12 //手机号以13, 15,18开头,八个 \d 数字字符 13 NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$"; 14 NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex]; 15 return [phoneTest evaluateWithObject:mobile]; 16 } 17 18 //车牌号验证 19 + (BOOL) validateCarNo:(NSString *)carNo 20 { 21 NSString *carRegex = @"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$"; 22 NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex]; 23 NSLog(@"carTest is %@",carTest); 24 return [carTest evaluateWithObject:carNo]; 25 } 26 27 //车型 28 + (BOOL) validateCarType:(NSString *)CarType 29 { 30 NSString *CarTypeRegex = @"^[\u4E00-\u9FFF]+$"; 31 NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CarTypeRegex]; 32 return [carTest evaluateWithObject:CarType]; 33 } 34 35 //用户名 36 + (BOOL) validateUserName:(NSString *)name 37 { 38 NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$"; 39 NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex]; 40 BOOL B = [userNamePredicate evaluateWithObject:name]; 41 return B; 42 } 43 44 //密码 45 + (BOOL) validatePassword:(NSString *)passWord 46 { 47 NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$"; 48 NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex]; 49 return [passWordPredicate evaluateWithObject:passWord]; 50 } 51 52 //昵称 53 + (BOOL) validateNickname:(NSString *)nickname 54 { 55 NSString *nicknameRegex = @"^[\u4e00-\u9fa5]{4,8}$"; 56 NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex]; 57 return [passWordPredicate evaluateWithObject:nickname]; 58 } 59 60 //身份证号 61 + (BOOL) validateIdentityCard: (NSString *)identityCard 62 { 63 BOOL flag; 64 if (identityCard.length <= 0) { 65 flag = NO; 66 return flag; 67 } 68 NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$"; 69 NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2]; 70 return [identityCardPredicate evaluateWithObject:identityCard]; 71 }
标签:
原文地址:http://www.cnblogs.com/1024Planet/p/4718395.html