常用正则整理

1.手机号(建议后台校验)

/** 正则表达式验证手机号,较为严格,运营商增加号段后本地要改 */

+ (BOOL)validateMobile:(NSString *)mobile;

+ (BOOL)validateMobile:(NSString *)mobile

{

    // 130-139  150-159除154,  180-189  145,147,149  170,171,173,175,176,177,178

    

    NSString *phoneRegex = @"^((13[0-9])|(15[012356789])|(18[0-9])|(14[579])|(17[0135678]))\\d{8}$";

    

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

    

    BOOL    isMobile = [phoneTest evaluateWithObject:mobile];

 

    return isMobile;

}

/** 正则表达式验证密码格式,限制为8-20位数字或字母 */

+ (BOOL)validatePassWord:(NSString *)strWord;

+ (BOOL)validatePassWord:(NSString *)strWord

{

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

    

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

    

    BOOL    isPass = [phoneTest evaluateWithObject:strWord];

 

    return isPass;

}

你可能感兴趣的:(常用正则整理)