正則表示式身份證、日期、電話號碼等
阿新 • • 發佈:2019-02-19
正則表示式
**身份證驗證:
方式一:只能判斷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;
}