正则表达式验证用户名、密码、手机号码、身份证(推荐)
废话不多说了,下面给大家介绍使用正则表达式验证用户名、密码、手机号码、身份证的写法,需要的的朋友参考下吧
//用户名 + (BOOL) validateUserName:(NSString *)name { NSString *userNameRegex = @"^[A-Za-z0-9]{3,20}+$"; NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex]; BOOL B = [userNamePredicate evaluateWithObject:name]; return B; } //密码 + (BOOL) validatePassword:(NSString *)passWord { NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$"; NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex]; return [passWordPredicate evaluateWithObject:passWord]; } //判断手机号码格式是否正确 + (BOOL)valiMobile:(NSString *)mobile { mobile = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""]; if (mobile.length != 11) { return NO; }else{ /** * 移动号段正则表达式 */ NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$"; /** * 联通号段正则表达式 */ NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$"; /** * 电信号段正则表达式 */ NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$"; NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM]; BOOL isMatch1 = [pred1 evaluateWithObject:mobile]; NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM]; BOOL isMatch2 = [pred2 evaluateWithObject:mobile]; NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM]; BOOL isMatch3 = [pred3 evaluateWithObject:mobile]; if (isMatch1 || isMatch2 || isMatch3) { return YES; }else{ return NO; } } } /** * 验证身份证号码是否正确的方法 * * @param IDNumber 传进身份证号码字符串 * * @return 返回YES或NO表示该身份证号码是否符合国家标准 */ + (BOOL)isCorrect:(NSString *)IDNumber { NSMutableArray *IDArray = [NSMutableArray array]; // 遍历身份证字符串,存入数组中 if (IDNumber.length == 18) { for (int i = 0; i < 18; i++) { NSRange range = NSMakeRange(i, 1); NSString *subString = [IDNumber substringWithRange:range]; [IDArray addObject:subString]; } }else{ for (int i = 0; i < 15; i++) { NSRange range = NSMakeRange(i, 1); NSString *subString = [IDNumber substringWithRange:range]; [IDArray addObject:subString]; } } // 系数数组 NSArray *coefficientArray = [NSArray arrayWithObjects:@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2", nil]; // 余数数组 NSArray *remainderArray = [NSArray arrayWithObjects:@"1", @"0", @"X", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2", nil]; // 每一位身份证号码和对应系数相乘之后相加所得的和 int sum = 0; if (IDNumber.length == 18) { for (int i = 0; i < 17; i++) { int coefficient = [coefficientArray[i] intValue]; int ID = [IDArray[i] intValue]; sum += coefficient * ID; } }else{ for (int i = 0; i < 14; i++) { int coefficient = [coefficientArray[i] intValue]; int ID = [IDArray[i] intValue]; sum += coefficient * ID; } } // 这个和除以11的余数对应的数 NSString *str = remainderArray[(sum % 11)]; // 身份证号码最后一位 NSString *string; if (IDNumber.length == 18) { string = [IDNumber substringFromIndex:17]; }else{ string = [IDNumber substringFromIndex:14]; } // 如果这个数字和身份证最后一位相同,则符合国家标准,返回YES if ([str isEqualToString:string]) { return YES; } else { return NO; } }
以上所述是小编给大家介绍的正则表达式验证用户名、密码、手机号码、身份证,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
赞 (0)