Home > Web Front-end > JS Tutorial > body text

How to verify the user's account password, mobile phone number and ID card using regular expressions

php中世界最好的语言
Release: 2018-03-29 15:00:24
Original
1860 people have browsed it

这次给大家带来用正则验证用户的帐号密码以及手机号码与身份证的方法,用正则验证用户的帐号密码以及手机号码与身份证的注意事项有哪些,下面就是实战案例,一起来看一下。

废话不多说了,下面给大家介绍使用正则表达式验证用户名、密码、手机号码、身份证的写法,需要的的朋友参考下吧

//用户名
+ (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;
 }
}
Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

在JS里编写简单的正则方式

经常会用到的15个前端表单验证的正则

The above is the detailed content of How to verify the user's account password, mobile phone number and ID card using regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!