iOS开发验证判断语句之正则表达式小结
程序员文章站
2023-12-20 23:07:28
前言
大家都知道开发一直离不开一些常用验证格式,比如:邮箱,手机号等等,在开发过程中一般都会新建一个工具类,专门管理这些验证方式,简便开发过程。一般都采用正则表达式的形式...
前言
大家都知道开发一直离不开一些常用验证格式,比如:邮箱,手机号等等,在开发过程中一般都会新建一个工具类,专门管理这些验证方式,简便开发过程。一般都采用正则表达式的形式来做判断,本文列举一些项目中非常实用的一些正则表达式的判断语句,以后开发起来直接复制粘贴就可以,这样大大节省了开发时间,下面不多说,直接上代码。
一、验证移动手机号:
+ (bool)ismobile:(nsstring*)mobile { nsstring *regex = @"^1+[34578]+\d{9}"; nspredicate *pred = [nspredicate predicatewithformat:@"self matches %@", regex]; return [pred evaluatewithobject:mobile]; }
二、邮箱验证:
+ (bool)isemail:(nsstring*)email { nsstring *regex = @"^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"; nspredicate *pred = [nspredicate predicatewithformat:@"self matches %@", regex]; return [pred evaluatewithobject:email]; }
三、整形验证
+ (bool)ispureint:(nsstring*)string{ nsscanner* scan = [nsscanner scannerwithstring:string]; int val; return[scan scanint:&val] && [scan isatend]; }
四、浮点型验证
- (bool)ispurefloat:(nsstring*)string{ nsscanner* scan = [nsscanner scannerwithstring:string]; float val; return[scan scanfloat:&val] && [scan isatend]; }
五、中文验证
+ (bool)ischinese:(nsstring*)chinese { nsstring *regex = @"^[\u4e00-\u9fa5]"; nspredicate *pred = [nspredicate predicatewithformat:@"self matches %@", regex]; return [pred evaluatewithobject: chinese]; }
六、网址url验证
+ (bool)isurl:(nsstring*)url { nsstring *regex = @"[a-za-z]+://[^\s]*"; nspredicate *pred = [nspredicate predicatewithformat:@"self matches %@", regex]; return [pred evaluatewithobject: url]; }
七、身份证号验证(比较繁琐)
+ (bool)verifyidcardnumber:(nsstring *)idcardnumber { idcardnumber = [idcardnumber stringbytrimmingcharactersinset:[nscharacterset whitespaceandnewlinecharacterset]]; if ([idcardnumber length] != 18) { return no; } nsstring *mmdd = @”(((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])))”; nsstring *leapmmdd = @”0229”; nsstring *year = @”(19|20)[0-9]{2}”; nsstring *leapyear = @”(19|20)(0[48]|[2468][048]|[13579][26])”; nsstring *yearmmdd = [nsstring stringwithformat:@”%@%@”, year, mmdd]; nsstring *leapyearmmdd = [nsstring stringwithformat:@”%@%@”, leapyear, leapmmdd]; nsstring *yyyymmdd = [nsstring stringwithformat:@”((%@)|(%@)|(%@))”, yearmmdd, leapyearmmdd, @”20000229”]; nsstring *area = @”(1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|82|[7-9]1)[0-9]{4}”; nsstring *regex = [nsstring stringwithformat:@”%@%@%@”, area, yyyymmdd , @”[0-9]{3}[0-9xx]”]; nspredicate *regextest = [nspredicate predicatewithformat:@"self matches %@", regex]; if (![regextest evaluatewithobject:idcardnumber]) { return no; } int summary = ([idcardnumber substringwithrange:nsmakerange(0,1)].intvalue + [idcardnumber substringwithrange:nsmakerange(10,1)].intvalue) *7 + ([idcardnumber substringwithrange:nsmakerange(1,1)].intvalue + [idcardnumber substringwithrange:nsmakerange(11,1)].intvalue) *9 + ([idcardnumber substringwithrange:nsmakerange(2,1)].intvalue + [idcardnumber substringwithrange:nsmakerange(12,1)].intvalue) *10 + ([idcardnumber substringwithrange:nsmakerange(3,1)].intvalue + [idcardnumber substringwithrange:nsmakerange(13,1)].intvalue) *5 + ([idcardnumber substringwithrange:nsmakerange(4,1)].intvalue + [idcardnumber substringwithrange:nsmakerange(14,1)].intvalue) *8 + ([idcardnumber substringwithrange:nsmakerange(5,1)].intvalue + [idcardnumber substringwithrange:nsmakerange(15,1)].intvalue) *4 + ([idcardnumber substringwithrange:nsmakerange(6,1)].intvalue + [idcardnumber substringwithrange:nsmakerange(16,1)].intvalue) *2 + [idcardnumber substringwithrange:nsmakerange(7,1)].intvalue *1 + [idcardnumber substringwithrange:nsmakerange(8,1)].intvalue *6 + [idcardnumber substringwithrange:nsmakerange(9,1)].intvalue *3; nsinteger remainder = summary % 11; nsstring *checkbit = @""; nsstring *checkstring = @"10x98765432"; checkbit = [checkstring substringwithrange:nsmakerange(remainder,1)];// 判断校验位 return [checkbit isequaltostring:[[idcardnumber substringwithrange:nsmakerange(17,1)] uppercasestring]]; }
八、一般用户名的验证
+ (bool) isusername:(nsstring *)name { nsstring *usernameregex = @"^[a-za-z0-9]{6,20}+$"; nspredicate *usernamepredicate = [nspredicate predicatewithformat:@"self matches %@",usernameregex]; return [usernamepredicate evaluatewithobject:name]; }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。
推荐阅读