有时候我们需要判断填写的用户手机号码、邮箱等信息是否出错并作出提示,这里我列举一些常用的方法供给大家探讨使用。
正则判断手机号码地址格式
- (BOOL)isMobileNumber:(NSString*)mobileNum {
NSString*MOBILE =@"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[06-8])\\d{8}$";
NSPredicate*regextestmobile = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", MOBILE];
return[regextestmobileevaluateWithObject:mobileNum];
}
邮箱判断
+ (BOOL) validateEmail:(NSString *)email{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:email];
}
验证身份证号码
+ (BOOL) validateIdentityCard: (NSString *)identityCard
{
BOOL flag;
if (identityCard.length <= 0) {
flag = NO;
return flag;
}
NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
return [identityCardPredicate evaluateWithObject:identityCard];
}
网友评论