废话不多说了,下面给大家介绍使用正则表达式验证用户名、密码、手机号码、身份证的写法,需要的的朋友参考下吧
//用户名 + (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",nil]; // 余数数组 NSArray *remainderArray = [NSArray arrayWithObjects:@"1",@"0",@"X",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; } }
以上所述是小编给大家介绍的正则表达式验证用户名、密码、手机号码、身份证,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。