下面的三个方法就是非常方便的快速判断的输入的是否为手机号,邮箱或者身份证号的方法,只需要调用这些方法,然后用一个bool值去接收返回的参数,然后进行判断就可以了。
//邮箱
+ (BOOL)validateEmail:(NSString*)email
{
NSString*emailRegex =@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate*emailTest = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",emailRegex];
return[emailTestevaluateWithObject:email];
}
//手机号码验证
+ (BOOL)validateMobile:(NSString*)mobile
{
//手机号以17, 13, 15,18开头,八个 \d 数字字符
NSString*phoneRegex =@"^((17[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
NSPredicate*phoneTest = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return[phoneTestevaluateWithObject:mobile];
}
//身份证号
+ (BOOL)validateIdentityCard: (NSString*)identityCard
{
BOOLflag;
if(identityCard.length<=0) {
flag =NO;
returnflag;
}
NSString*regex2=@"^(\\d{14}|\\d{17})(\\d|[xX])$";
NSPredicate*identityCardPredicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex2];
return[identityCardPredicateevaluateWithObject:identityCard];
}