欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

正则表达式验证用户名、密码、手机号码、身份证(推荐)

程序员文章站 2022-06-23 11:31:58
废话不多说了,下面给大家介绍使用正则表达式验证用户名、密码、手机号码、身份证的写法,需要的的朋友参考下吧 //用户名 + (bool) validateuser...

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

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

以上所述是小编给大家介绍的正则表达式验证用户名、密码、手机号码、身份证,希望对大家有所帮助