目前,只遇到身份证,手机号,工作单位电话号,银行卡号,以后遇到其他的会陆续添加。。。下面开车
身份证:
//身份证号的正则表达
-(BOOL)validateIdentityCard: (NSString *)identityCard
{
if (identityCard.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:[identityCard 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+=[[identityCard substringWithRange:NSMakeRange(i , 1) ] intValue]* [[codeArray objectAtIndex:i] intValue];
}
NSString* strlast = [checkCodeDic objectForKey:[NSString stringWithFormat:@"%d",sumValue%11]];
if ([strlast isEqualToString: [[identityCard substringWithRange:NSMakeRange(17, 1)]uppercaseString]]) {
return YES;
}
return NO;
}
手机号:
//手机号码的正则表达式
- (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;
}
}
}
银行卡号:
//银行卡号的正则表达(位数)
-(BOOL)validateBankCardNumber: (NSString *)bankCardNumber
{
BOOL flag;
if (bankCardNumber.length <= 0) {
flag = NO;
return flag;
}
NSString *regex2 = @"^(\\d{15,30})";
NSPredicate *bankCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
return [bankCardPredicate evaluateWithObject:bankCardNumber];
}
工作单位电话,加区号:
//工作单位电话正则验证
-(BOOL)validatetelenumb: (NSString *)telenumb
{
BOOL flag;
if (telenumb.length <= 0) {
flag = NO;
return flag;
}
NSString *regex2 = @"^(\\d{3,4})\\d{7,8}$";
NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
return [identityCardPredicate evaluateWithObject:telenumb];
}
未完待续。。。