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

时间:2022-05-20 14:01:41

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//用户名
+ (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;
 }
}

以上所述是小编给大家介绍的正则表达式验证用户名、密码、手机号码、身份证,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://blog.csdn.net/qq_33226881/article/details/73740205