正则表达式
**身份证验证:
方式一:只能判断18位身份证:但是方法优于方法二**
+ (BOOL)checkIdentityCardNo:(NSString*)cardNo
{
if (cardNo.length != 18) {
return NO;
}
NSArray* codeArray = [NSArray arrayWithObjects:@"7",@"9",@"10",@"5",@"8",@"4",@"2",@"1",@"6",@"3",@"7",@"9",@"10",@"5",@"8",@"4",@"2", nil];
NSDictionary* checkCodeDic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1",@"0",@"X",@"9",@"8",@"7",@"6",@"5",@"4",@"3",@"2", nil] forKeys:[NSArray arrayWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil]];
NSScanner* scan = [NSScanner scannerWithString:[cardNo substringToIndex:17]];
int val;
BOOL isNum = [scan scanInt:&val] && [scan isAtEnd];
if (!isNum) {
return NO;
}
int sumValue = 0;
for (int i =0; i<17; i++) {
sumValue+=[[cardNo substringWithRange:NSMakeRange(i , 1) ] intValue]* [[codeArray objectAtIndex:i] intValue];
}
NSString* strlast = [checkCodeDic objectForKey:[NSString stringWithFormat:@"%d",sumValue%11]];
if ([strlast isEqualToString: [[cardNo substringWithRange:NSMakeRange(17, 1)]uppercaseString]]) {
return YES;
}
return NO;
}
方式二:判断18位15位身份证
+ (BOOL)PINpersonalIdentificationNumber:(NSString *) textString
{
NSString* number=@"(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])";
NSPredicate *numberPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",number];
if ([numberPre evaluateWithObject:textString]) {
NSRange range1 = NSMakeRange(0, 1);
NSInteger one = [[textString substringWithRange:range1] integerValue];
if (one < 9 && one > 0) {
NSRange range2 = NSMakeRange(1, 1);
NSInteger two = [[textString substringWithRange:range2] integerValue];
if (two < 8 && two >= 0) {
NSRange range3 = NSMakeRange(6, 4);
NSString *year = [textString substringWithRange:range3];
NSRange range4 = NSMakeRange(10, 2);
NSString *month = [textString substringWithRange:range4];
NSRange range5 = NSMakeRange(12, 2);
NSString *day = [textString substringWithRange:range5];
NSString *DateText = [NSString stringWithFormat:@"%@-%@-%@",year,month,day];
NSString *Date = @"(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((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]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)";
NSPredicate *numberPre1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",Date];
if ([numberPre1 evaluateWithObject:DateText]) {
return YES;
}else{
return NO;
}
}else{
return NO;
}
}else{
return NO;
}
}else{
return NO;
}
}
日期验证:
闰年的2月份有29天,因此匹配闰年日期格式为YYYY-MM-DD的正则表达式为:
(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29
最后,将平年和闰年的日期验证表达式合并,我们得到最终的验证日期格式为YYYY-MM-DD的正则表达式为:
(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((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]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)
DD/MM/YYYY格式的正则验证表达式为:
(((0[1-9]|[12][0-9]|3[01])/((0[13578]|1[02]))|((0[1-9]|[12][0-9]|30)/(0[469]|11))|(0[1-9]|[1][0-9]|2[0-8])/(02))/([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3}))|(29/02/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))
电话号码验证:
+ (BOOL) isMobile:(NSString *)mobileNumbel{
/**
* 手机号码
* 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
* 联通:130,131,132,152,155,156,185,186
* 电信:133,1349,153,180,189,181(增加)
*/
NSString * MOBIL = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
/**
10 * 中国移动:China Mobile
11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
12 */
NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[2378])\\d)\\d{7}$";
/**
15 * 中国联通:China Unicom
16 * 130,131,132,152,155,156,185,186
17 */
NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
/**
20 * 中国电信:China Telecom
21 * 133,1349,153,180,189,181(增加)
22 */
NSString * CT = @"^1((33|53|8[019])[0-9]|349)\\d{7}$";
NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBIL];
NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
if (([regextestmobile evaluateWithObject:mobileNumbel]
|| [regextestcm evaluateWithObject:mobileNumbel]
|| [regextestct evaluateWithObject:mobileNumbel]
|| [regextestcu evaluateWithObject:mobileNumbel])) {
return YES;
}
return NO;
}