码迷,mamicode.com
首页 > 移动开发 > 详细

iOS - 验证输入的是否是正确的身份证号码和手机号码

时间:2015-09-28 13:10:03      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

 

- (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;

}

 

 

- (BOOL)isMobileNumber:(NSString *)mobileNumString

{

    NSString *pattern = @"^1+[3578]+\\d{9}";

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];

    BOOL isMatch = [pred evaluateWithObject:mobileNumString];

    return isMatch;

}

 

iOS - 验证输入的是否是正确的身份证号码和手机号码

标签:

原文地址:http://www.cnblogs.com/Cheetah-yang/p/4843744.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!