iOS开发验证判断语句之正则表达式小结
前言
大家都知道开发一直离不开一些常用验证格式,比如:邮箱,手机号等等,在开发过程中一般都会新建一个工具类,专门管理这些验证方式,简便开发过程。一般都采用正则表达式的形式来做判断,本文列举一些项目中非常实用的一些正则表达式的判断语句,以后开发起来直接复制粘贴就可以,这样大大节省了开发时间,下面不多说,直接上代码。
一、验证移动手机号:
+(BOOL)isMobile:(NSString*)mobile { NSString*regex=@"^1+[34578]+\d{9}"; NSPredicate*pred=[NSPredicatepredicateWithFormat:@"SELFMATCHES%@",regex]; return[predevaluateWithObject:mobile]; }
二、邮箱验证:
+(BOOL)isEmail:(NSString*)email { NSString*regex=@"^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"; NSPredicate*pred=[NSPredicatepredicateWithFormat:@"SELFMATCHES%@",regex]; return[predevaluateWithObject:email]; }
三、整形验证
+(BOOL)isPureInt:(NSString*)string{ NSScanner*scan=[NSScannerscannerWithString:string]; intval; return[scanscanInt:&val]&&[scanisAtEnd]; }
四、浮点型验证
-(BOOL)isPureFloat:(NSString*)string{ NSScanner*scan=[NSScannerscannerWithString:string]; floatval; return[scanscanFloat:&val]&&[scanisAtEnd]; }
五、中文验证
+(BOOL)isChinese:(NSString*)chinese { NSString*regex=@"^[\u4e00-\u9fa5]"; NSPredicate*pred=[NSPredicatepredicateWithFormat:@"SELFMATCHES%@",regex]; return[predevaluateWithObject:chinese]; }
六、网址URL验证
+(BOOL)isURL:(NSString*)url { NSString*regex=@"[a-zA-z]+://[^\s]*"; NSPredicate*pred=[NSPredicatepredicateWithFormat:@"SELFMATCHES%@",regex]; return[predevaluateWithObject:url]; }
七、身份证号验证(比较繁琐)
+(BOOL)verifyIDCardNumber:(NSString*)IDCardNumber { IDCardNumber=[IDCardNumberstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]]; if([IDCardNumberlength]!=18) { returnNO; } 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=[NSStringstringWithFormat:@”%@%@”,year,mmdd]; NSString*leapyearMmdd=[NSStringstringWithFormat:@”%@%@”,leapYear,leapMmdd]; NSString*yyyyMmdd=[NSStringstringWithFormat:@”((%@)|(%@)|(%@))”,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=[NSStringstringWithFormat:@”%@%@%@”,area,yyyyMmdd,@”[0-9]{3}[0-9Xx]”]; NSPredicate*regexTest=[NSPredicatepredicateWithFormat:@"SELFMATCHES%@",regex]; if(![regexTestevaluateWithObject:IDCardNumber]) { returnNO; } intsummary=([IDCardNumbersubstringWithRange:NSMakeRange(0,1)].intValue+[IDCardNumbersubstringWithRange:NSMakeRange(10,1)].intValue)*7 +([IDCardNumbersubstringWithRange:NSMakeRange(1,1)].intValue+[IDCardNumbersubstringWithRange:NSMakeRange(11,1)].intValue)*9 +([IDCardNumbersubstringWithRange:NSMakeRange(2,1)].intValue+[IDCardNumbersubstringWithRange:NSMakeRange(12,1)].intValue)*10 +([IDCardNumbersubstringWithRange:NSMakeRange(3,1)].intValue+[IDCardNumbersubstringWithRange:NSMakeRange(13,1)].intValue)*5 +([IDCardNumbersubstringWithRange:NSMakeRange(4,1)].intValue+[IDCardNumbersubstringWithRange:NSMakeRange(14,1)].intValue)*8 +([IDCardNumbersubstringWithRange:NSMakeRange(5,1)].intValue+[IDCardNumbersubstringWithRange:NSMakeRange(15,1)].intValue)*4 +([IDCardNumbersubstringWithRange:NSMakeRange(6,1)].intValue+[IDCardNumbersubstringWithRange:NSMakeRange(16,1)].intValue)*2 +[IDCardNumbersubstringWithRange:NSMakeRange(7,1)].intValue*1+[IDCardNumbersubstringWithRange:NSMakeRange(8,1)].intValue*6 +[IDCardNumbersubstringWithRange:NSMakeRange(9,1)].intValue*3; NSIntegerremainder=summary%11; NSString*checkBit=@""; NSString*checkString=@"10X98765432"; checkBit=[checkStringsubstringWithRange:NSMakeRange(remainder,1)];//判断校验位 return[checkBitisEqualToString:[[IDCardNumbersubstringWithRange:NSMakeRange(17,1)]uppercaseString]]; }
八、一般用户名的验证
+(BOOL)isUserName:(NSString*)name { NSString*userNameRegex=@"^[A-Za-z0-9]{6,20}+$"; NSPredicate*userNamePredicate=[NSPredicatepredicateWithFormat:@"SELFMATCHES%@",userNameRegex]; return[userNamePredicateevaluateWithObject:name]; }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。