相信大家再做登录/注册/实名认证等功能的时候会用到, 分享一下
通过谓词筛选 配合正则表达式 so easy!
//字数不限,只允许是汉字
@"^[\u4e00-\u9fa5]{0,}";
//'真实姓名'正则表达式筛选
NSString *realNamePattern = @"^[\u4e00-\u9fa5]{0,}";
NSPredicate *realNamePredicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",realNamePattern];
if(![realNamePredicate evaluateWithObject:weakSelf.realNameContent]){
NSLog(@"姓名格式不正确,请检查后重试!");
}
//限制字数18位, 支持0-9的所有数字 最后以为可以是x或者X 大小写都支持
@"\\d{17}[[0-9],0-9xX]"
//'身份证号'正则表达式筛选
NSString *identificationNumberPattern =@"\\d{17}[[0-9],0-9xX]";
NSPredicate *identificationNumberPredicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",identificationNumberPattern];
if(![identificationNumberPredicateevaluateWithObject:weakSelf.identificationNumberContent]){
NSLog(@"身份证格式不正确,请检查后重试!");
}