正则匹配
#import
@interface Utils : NSObject
#pragma 正则匹配手机号
+ (BOOL)checkTelNumber:(NSString *) telNumber;
#pragma 正则匹配用户密码6-18位数字和字母组合
+ (BOOL)checkPassword:(NSString *) password;
#pragma 正则匹配用户姓名,20位的中文或英文
+ (BOOL)checkUserName : (NSString *) userName;
#pragma 正则匹配用户身份证号
+ (BOOL)checkUserIdCard: (NSString *) idCard;
#pragma 正则匹员工号,12位的数字
+ (BOOL)checkEmployeeNumber : (NSString *) number;
#pragma 正则匹配URL
+ (BOOL)checkURL : (NSString *) url;
@end
.m实现文件:
//// Utils.m// AutoSizing//// Created by yb on 15/2/7.// Copyright (c) 2015年http://blog.csdn.net/yangbingbinga. All rights reserved.//#import Utils.h@implementation Utils#pragma 正则匹配手机号+ (BOOL)checkTelNumber:(NSString *) telNumber{ NSString *pattern = @^1+[3578]+\d{9}; NSPredicate *pred = [NSPredicate predicateWithFormat:@SELF MATCHES %@, pattern]; BOOL isMatch = [pred evaluateWithObject:telNumber]; return isMatch;}#pragma 正则匹配用户密码6-18位数字和字母组合+ (BOOL)checkPassword:(NSString *) password{ NSString *pattern = @^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,18}; NSPredicate *pred = [NSPredicate predicateWithFormat:@SELF MATCHES %@, pattern]; BOOL isMatch = [pred evaluateWithObject:password]; return isMatch; }#pragma 正则匹配用户姓名,20位的中文或英文+ (BOOL)checkUserName : (NSString *) userName{ NSString *pattern = @^[a-zA-Z一-龥]{1,20}; NSPredicate *pred = [NSPredicate predicateWithFormat:@SELF MATCHES %@, pattern]; BOOL isMatch = [pred evaluateWithObject:userName]; return isMatch; }#pragma 正则匹配用户身份证号15或18位+ (BOOL)checkUserIdCard: (NSString *) idCard{ NSString *pattern = @(^[0-9]{15}$)|([0-9]{17}([0-9]|X)$); NSPredicate *pred = [NSPredicate predicateWithFormat:@SELF MATCHES %@, pattern]; BOOL isMatch = [pred evaluateWithObject:idCard]; return isMatch;}#pragma 正则匹员工号,12位的数字+ (BOOL)checkEmployeeNumber : (NSString *) number{ NSString *pattern = @^[0-9]{12}; NSPredicate *pred = [NSPredicate predicateWithFormat:@SELF MATCHES %@, pattern]; BOOL isMatch = [pred evaluateWithObject:number]; return isMatch; }#pragma 正则匹配URL+ (BOOL)checkURL : (NSString *) url{ NSString *pattern = @^[0-9A-Za-z]{1,50}; NSPredicate *pred = [NSPredicate predicateWithFormat:@SELF MATCHES %@, pattern]; BOOL isMatch = [pred evaluateWithObject:url]; return isMatch; }@end
#pragma mark -正则表达式身份证校验
-(BOOL) validateIdentityCard: (NSString*)IDCardNumber
{
IDCardNumber = [IDCardNumberstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]];
if([IDCardNumberlength] !=18)
{
NSLog(@"0");
returnNO;
}
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])))";
NSString*leapMmdd =@"0229";
NSString*year =@"(19|20)[0-9]{2}";
NSString*leapYear =@"(19|20)(0[48]|[2468][048]|[13579][26])";
NSString*yearMmdd = [NSStringstringWithFormat:@"%@%@", year, mmdd];
NSString*leapyearMmdd = [NSStringstringWithFormat:@"%@%@", leapYear, leapMmdd];
NSString*yyyyMmdd = [NSStringstringWithFormat:@"((%@)|(%@)|(%@))", yearMmdd, leapyearMmdd,@"20000229"];
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}";
NSString*regex = [NSStringstringWithFormat:@"%@%@%@", area, yyyyMmdd ,@"[0-9]{3}[0-9Xx]"];
NSPredicate*regexTest = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", regex];
if(![regexTestevaluateWithObject:IDCardNumber])
{
NSLog(@"0");
returnNO;
}
intsummary = ([IDCardNumbersubstringWithRange:NSMakeRange(0,1)].intValue+ [IDCardNumbersubstringWithRange:NSMakeRange(10,1)].intValue) *7
+ ([IDCardNumbersubstringWithRange:NSMakeRange(1,1)].intValue+ [IDCardNumbersubstringWithRange:NSMakeRange(11,1)].intValue) *9
+ ([IDCardNumbersubstringWithRange:NSMakeRange(2,1)].intValue+ [IDCardNumbersubstringWithRange:NSMakeRange(12,1)].intValue) *10
+ ([IDCardNumbersubstringWithRange:NSMakeRange(3,1)].intValue+ [IDCardNumbersubstringWithRange:NSMakeRange(13,1)].intValue) *5
+ ([IDCardNumbersubstringWithRange:NSMakeRange(4,1)].intValue+ [IDCardNumbersubstringWithRange:NSMakeRange(14,1)].intValue) *8
+ ([IDCardNumbersubstringWithRange:NSMakeRange(5,1)].intValue+ [IDCardNumbersubstringWithRange:NSMakeRange(15,1)].intValue) *4
+ ([IDCardNumbersubstringWithRange:NSMakeRange(6,1)].intValue+ [IDCardNumbersubstringWithRange:NSMakeRange(16,1)].intValue) *2
+ [IDCardNumbersubstringWithRange:NSMakeRange(7,1)].intValue*1+ [IDCardNumbersubstringWithRange:NSMakeRange(8,1)].intValue*6
+ [IDCardNumbersubstringWithRange:NSMakeRange(9,1)].intValue*3;
NSIntegerremainder = summary %11;
NSString*checkBit =@"";
NSString*checkString =@"10X98765432";
checkBit = [checkStringsubstringWithRange:NSMakeRange(remainder,1)];//判断校验位
[selfMBProgressHUDShowTextandTitle:[NSStringstringWithFormat:@"%d ",[checkBitisEqualToString:[[IDCardNumbersubstringWithRange:NSMakeRange(17,1)]uppercaseString]]]];
NSLog(@"%d....%@",[checkBitisEqualToString:[[IDCardNumbersubstringWithRange:NSMakeRange(17,1)]uppercaseString]],@"ss");
return[checkBitisEqualToString:[[IDCardNumbersubstringWithRange:NSMakeRange(17,1)]uppercaseString]];
}