码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 正则表达式判断邮箱、身份证..是否正确

时间:2016-08-04 13:31:58      阅读:334      评论:0      收藏:0      [点我收藏+]

标签:

//邮箱

+ (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) validateMobile:(NSString *)mobile

{

    //手机号以13 1518开头,八个 \d 数字字符

    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";

    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];

    return [phoneTest evaluateWithObject:mobile];

}

 

 

//车牌号验证

+ (BOOL) validateCarNo:(NSString *)carNo

{

    NSString *carRegex = @"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$";

    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];

    NSLog(@"carTest is %@",carTest);

    return [carTest evaluateWithObject:carNo];

}

 

 

//车型

+ (BOOL) validateCarType:(NSString *)CarType

{

    NSString *CarTypeRegex = @"^[\u4E00-\u9FFF]+$";

    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CarTypeRegex];

    return [carTest evaluateWithObject:CarType];

}

 

 

//用户名

+ (BOOL) validateUserName:(NSString *)name

{

    NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$";

    NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];

    BOOL B = [userNamePredicate evaluateWithObject:name];

    return B;

}

 

 

//密码

+ (BOOL) validatePassword:(NSString *)passWord

{

    NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";

    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];

    return [passWordPredicate evaluateWithObject:passWord];

}

 

 

//昵称

+ (BOOL) validateNickname:(NSString *)nickname

{

    NSString *nicknameRegex = @"^[\u4e00-\u9fa5]{4,8}$";

    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];

    return [passWordPredicate evaluateWithObject:nickname];

}

 

 

//身份证号

+ (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];

}

 

/**

 检查身份证名字

 */

 

-(BOOL)checkUserName : (NSString *) userName

{

    NSString *pattern = @"^[a-zA-Z\u4E00-\u9FA5]{1,20}";

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];

    BOOL isMatch = [pred evaluateWithObject:userName];

    return isMatch;

    

}

 

 

#pragma 正则匹配URL

+ (BOOL)checkURL : (NSString *) url

{

    NSString *pattern = @"^[0-9A-Za-z]{1,50}";

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];

    BOOL isMatch = [pred evaluateWithObject:url];

    return isMatch;

    

}

iOS 正则表达式判断邮箱、身份证..是否正确

标签:

原文地址:http://www.cnblogs.com/soulDn/p/5736207.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!