您好,登錄后才能下訂單哦!
在某些App應用里面需要填寫用戶信息(用于驗證),有身份證、手機號、軍官證等等。下面來跟大家分享一下各自的驗證方式。
1.驗證 手機號碼 ,這個是對任意輸入的一串數字做驗證,返回一個識別結果(字符串),根據這個結果再做判斷。
//判斷手機號碼 + (NSString *)valiMobile:(NSString *)mobile{ if (mobile.length != 11){ return @"手機號碼格式錯誤"; }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 isMatch2 = [pred1 evaluateWithObject:mobile]; NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM]; BOOL isMatch3 = [pred2 evaluateWithObject:mobile]; NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM]; BOOL isMatch4 = [pred3 evaluateWithObject:mobile]; if (isMatch2 || isMatch3 || isMatch4) { return nil; }else{ return @"請輸入正確的手機號碼"; } } return nil; }
2.驗證軍官證
//驗證軍官證 - (BOOL) checkOfficerCard: (NSString *) officerCard{ //exp:政字第00111206號 BOOL flag; if (officerCard.length <= 0) { flag = NO; return flag; } NSString *regex2 = @"^([\u5357|\u5317|\u6c88|\u5170|\u6210|\u6d4e|\u5e7f|\u6d77|\u7a7a|\u53c2|\u653f|\u540e|\u88c5][\u5b57][\u7b2c]\\d{8}[\u53f7])$"; NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2]; return [identityCardPredicate evaluateWithObject:officerCard]; }
3.驗證身份證,這個是 真實身份證 的驗證方法,具有唯一性。
//精確的身份證號碼有效性檢測 - (BOOL) accurateVerifyIDCardNumber:(NSString *)value { value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; int length =0; if (!value) { return NO; }else { length = (int)value.length; if (length !=15 && length !=18) { return NO; } } // 省份代碼 NSArray *areasArray =@[@"11",@"12", @"13",@"14", @"15",@"21", @"22",@"23", @"31",@"32", @"33",@"34", @"35",@"36", @"37",@"41", @"42",@"43", @"44",@"45", @"46",@"50", @"51",@"52", @"53",@"54", @"61",@"62", @"63",@"64", @"65",@"71", @"81",@"82", @"91"]; NSString *valueStart2 = [value substringToIndex:2]; BOOL areaFlag =NO; for (NSString *areaCode in areasArray) { if ([areaCode isEqualToString:valueStart2]) { areaFlag =YES; break; } } if (!areaFlag) { return NO; } NSRegularExpression *regularExpression; NSUInteger numberofMatch; int year =0; switch (length) { case 15: year = [value substringWithRange:NSMakeRange(6,2)].intValue +1900; if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) { regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$" options:NSRegularExpressionCaseInsensitive error:nil];//測試出生日期的合法性 }else { regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$" options:NSRegularExpressionCaseInsensitive error:nil];//測試出生日期的合法性 } numberofMatch = [regularExpression numberOfMatchesInString:value options:NSMatchingReportProgress range:NSMakeRange(0, value.length)]; if(numberofMatch >0) { return YES; }else { return NO; } case 18: year = [value substringWithRange:NSMakeRange(6,4)].intValue; if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) { regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$" options:NSRegularExpressionCaseInsensitive error:nil];//測試出生日期的合法性 }else { regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$" options:NSRegularExpressionCaseInsensitive error:nil];//測試出生日期的合法性 } numberofMatch = [regularExpression numberOfMatchesInString:value options:NSMatchingReportProgress range:NSMakeRange(0, value.length)]; if(numberofMatch >0) { int S = ([value substringWithRange:NSMakeRange(0,1)].intValue + [value substringWithRange:NSMakeRange(10,1)].intValue) *7 + ([value substringWithRange:NSMakeRange(1,1)].intValue + [value substringWithRange:NSMakeRange(11,1)].intValue) *9 + ([value substringWithRange:NSMakeRange(2,1)].intValue + [value substringWithRange:NSMakeRange(12,1)].intValue) *10 + ([value substringWithRange:NSMakeRange(3,1)].intValue + [value substringWithRange:NSMakeRange(13,1)].intValue) *5 + ([value substringWithRange:NSMakeRange(4,1)].intValue + [value substringWithRange:NSMakeRange(14,1)].intValue) *8 + ([value substringWithRange:NSMakeRange(5,1)].intValue + [value substringWithRange:NSMakeRange(15,1)].intValue) *4 + ([value substringWithRange:NSMakeRange(6,1)].intValue + [value substringWithRange:NSMakeRange(16,1)].intValue) *2 + [value substringWithRange:NSMakeRange(7,1)].intValue *1 + [value substringWithRange:NSMakeRange(8,1)].intValue *6 + [value substringWithRange:NSMakeRange(9,1)].intValue *3; int Y = S %11; NSString *M =@"F"; NSString *JYM =@"10X98765432"; M = [JYM substringWithRange:NSMakeRange(Y,1)];// 判斷校驗位 if ([M isEqualToString:[value substringWithRange:NSMakeRange(17,1)]]) { return YES;// 檢測ID的校驗位 }else { return NO; } }else { return NO; } default: return NO; } }
4.郵箱的驗證
//郵箱驗證 - (BOOL)isEmailAddress{ NSString *emailRegex = @"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self matches %@",emailRegex]; return [predicate evaluateWithObject:self]; }
5.銀行卡有效性驗證。這里是給 NSString 寫的分類
/** 銀行卡號有效性問題Luhn算法 * 現行 16 位銀聯卡現行卡號開頭 6 位是 622126~622925 之間的,7 到 15 位是銀行自定義的, * 可能是發卡分行,發卡網點,發卡序號,第 16 位是校驗碼。 * 16 位卡號校驗位采用 Luhm 校驗方法計算: * 1,將未帶校驗位的 15 位卡號從右依次編號 1 到 15,位于奇數位號上的數字乘以 2 * 2,將奇位乘積的個十位全部相加,再加上所有偶數位上的數字 * 3,將加法和加上校驗位能被 10 整除。 */ - (BOOL)bankCardCheck{ NSString * lastNum = [[self substringFromIndex:(self.length-1)] copy];//取出最后一位 NSString * forwardNum = [[self substringToIndex:(self.length -1)] copy];//前15或18位 NSMutableArray * forwardArr = [[NSMutableArray alloc] initWithCapacity:0]; for (int i=0; i<forwardNum.length; i++) { NSString * subStr = [forwardNum substringWithRange:NSMakeRange(i, 1)]; [forwardArr addObject:subStr]; } NSMutableArray * forwardDescArr = [[NSMutableArray alloc] initWithCapacity:0]; for (int i = (int)(forwardArr.count-1); i> -1; i--) {//前15位或者前18位倒序存進數組 [forwardDescArr addObject:forwardArr[i]]; } NSMutableArray * arrOddNum = [[NSMutableArray alloc] initWithCapacity:0];//奇數位*2的積 < 9 NSMutableArray * arrOddNum2 = [[NSMutableArray alloc] initWithCapacity:0];//奇數位*2的積 > 9 NSMutableArray * arrEvenNum = [[NSMutableArray alloc] initWithCapacity:0];//偶數位數組 for (int i=0; i< forwardDescArr.count; i++) { NSInteger num = [forwardDescArr[i] intValue]; if (i%2) {//偶數位 [arrEvenNum addObject:[NSNumber numberWithInteger:num]]; }else{//奇數位 if (num * 2 < 9) { [arrOddNum addObject:[NSNumber numberWithInteger:num * 2]]; }else{ NSInteger decadeNum = (num * 2) / 10; NSInteger unitNum = (num * 2) % 10; [arrOddNum2 addObject:[NSNumber numberWithInteger:unitNum]]; [arrOddNum2 addObject:[NSNumber numberWithInteger:decadeNum]]; } } } __block NSInteger sumOddNumTotal = 0; [arrOddNum enumerateObjectsUsingBlock:^(NSNumber * obj, NSUInteger idx, BOOL *stop) { sumOddNumTotal += [obj integerValue]; }]; __block NSInteger sumOddNum2Total = 0; [arrOddNum2 enumerateObjectsUsingBlock:^(NSNumber * obj, NSUInteger idx, BOOL *stop) { sumOddNum2Total += [obj integerValue]; }]; __block NSInteger sumEvenNumTotal =0 ; [arrEvenNum enumerateObjectsUsingBlock:^(NSNumber * obj, NSUInteger idx, BOOL *stop) { sumEvenNumTotal += [obj integerValue]; }]; NSInteger lastNumber = [lastNum integerValue]; NSInteger luhmTotal = lastNumber + sumEvenNumTotal + sumOddNum2Total + sumOddNumTotal; return (luhmTotal%10 ==0)?YES:NO; }
以上簡單的收錄了在iOS開發中常見的正則表達式的驗證方法,如果寫的有問題,還請大家及時指正。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。