iOS中常见正则表达式验证方法
程序员文章站
2023-12-19 18:50:16
在某些app应用里面需要填写用户信息(用于验证),有身份证、手机号、军官证等等。下面来跟大家分享一下各自的验证方式。
1.验证 手机号码 ,这个是对任意输入的一串数字做验...
在某些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 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 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开发中常见的正则表达式的验证方法,如果写的有问题,还请大家及时指正。