小编给大家分享一下正则表达式如何实现验证用户名、密码、手机号码、身份证功能,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
示例
//用户名 + (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 isMatch2 = [pred1 evaluateWithObject:mobile]; NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM]; BOOL isMatch3 = [pred2 evaluateWithObject:mobile]; NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM]; BOOL isMatch4 = [pred3 evaluateWithObject:mobile]; if (isMatch2 || isMatch3 || isMatch4) { 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; } }
以上是“正则表达式如何实现验证用户名、密码、手机号码、身份证功能”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。