iOS开发常用校验
一、身份证号码校验
1 + (BOOL)cheakIdentityCard: (NSString *)value 2 { 3 value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 4 if ([value length] != 18) { 5 return NO; 6 } 7 8 NSString *mmdd = @"(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8])))"; 9 NSString *leapMmdd = @"0229"; 10 NSString *year = @"(19|20)[0-9]{2}"; 11 NSString *leapYear = @"(19|20)(0[48]|[2468][048]|[13579][26])"; 12 NSString *yearMmdd = [NSString stringWithFormat:@"%@%@", year, mmdd]; 13 NSString *leapyearMmdd = [NSString stringWithFormat:@"%@%@", leapYear, leapMmdd]; 14 NSString *yyyyMmdd = [NSString stringWithFormat:@"((%@)|(%@)|(%@))", yearMmdd, leapyearMmdd, @"20000229"]; 15 NSString *area = @"(1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|82|[7-9]1)[0-9]{4}"; 16 NSString *regex = [NSString stringWithFormat:@"%@%@%@", area, yyyyMmdd , @"[0-9]{3}[0-9Xx]"]; 17 NSPredicate *regexTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; 18 if (![regexTest evaluateWithObject:value]) { 19 return NO; 20 } 21 22 int summary = ([value substringWithRange:NSMakeRange(0,1)].intValue + 23 [value substringWithRange:NSMakeRange(10,1)].intValue) *7 + 24 ([value substringWithRange:NSMakeRange(1,1)].intValue + 25 [value substringWithRange:NSMakeRange(11,1)].intValue) *9 + 26 ([value substringWithRange:NSMakeRange(2,1)].intValue + 27 [value substringWithRange:NSMakeRange(12,1)].intValue) *10 + 28 ([value substringWithRange:NSMakeRange(3,1)].intValue + 29 [value substringWithRange:NSMakeRange(13,1)].intValue) *5 + 30 ([value substringWithRange:NSMakeRange(4,1)].intValue + 31 [value substringWithRange:NSMakeRange(14,1)].intValue) *8 + 32 ([value substringWithRange:NSMakeRange(5,1)].intValue + 33 [value substringWithRange:NSMakeRange(15,1)].intValue) *4 + 34 ([value substringWithRange:NSMakeRange(6,1)].intValue + 35 [value substringWithRange:NSMakeRange(16,1)].intValue) *2 + 36 [value substringWithRange:NSMakeRange(7,1)].intValue *1 + 37 [value substringWithRange:NSMakeRange(8,1)].intValue *6 + 38 [value substringWithRange:NSMakeRange(9,1)].intValue *3; 39 40 NSInteger remainder = summary % 11; 41 NSString *checkBit = @""; 42 NSString *checkString = @"10X98765432"; 43 checkBit = [checkString substringWithRange:NSMakeRange(remainder,1)];// 判断校验位 44 return [checkBit isEqualToString:[[value substringWithRange:NSMakeRange(17,1)] uppercaseString]]; 45 46 }
二、手机号校验
1 + (BOOL)checkTelNumber:(NSString *) telNumber 2 { 3 NSString *pattern = @"^1+[3578]+\d{9}"; 4 5 NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; 6 BOOL isMatch = [pred evaluateWithObject:telNumber]; 7 return isMatch; 8 }
三、简单密码校验(6-18位数字和字母组合,第一位不能是数字开头)
1 + (BOOL)checkPassword:(NSString *) password 2 { 3 NSString *pattern = @"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,18}"; 4 NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; 5 BOOL isMatch = [pred evaluateWithObject:password]; 6 return isMatch; 7 }
四、用户姓名校验(20位的中文或英文)
1 + (BOOL)checkUserName : (NSString *) userName 2 { 3 NSString *pattern = @"^[a-zA-Z一-龥]{1,20}"; 4 NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; 5 BOOL isMatch = [pred evaluateWithObject:userName]; 6 return isMatch; 7 }
五、用户邮箱校验
1 + (BOOL) checkUserEmail:(NSString *)email 2 { 3 NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; 4 NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; 5 return [emailTest evaluateWithObject:email]; 6 }