开发锦集

常用手机号码及密码验证

2017-03-27  本文已影响10人  YvanLiu

检测是否是手机号码

- (BOOL)isMobileNumber:(NSString *)mobileNum {
    NSString *MOBILE = @"^1(3[0-9]|4[579]|5[0-35-9]|7[0135-8]|8[0-9])\\d{8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    return [regextestmobile evaluateWithObject:mobileNum];
}

检验是否含有大写字母

- (BOOL)isCapital:(NSString *)capital {
    NSString *CAPITAL = @"[A-Z]+";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CAPITAL];    
    BOOL isyes = [regextestmobile evaluateWithObject:capital];
    return isyes;
}

检验是否含有小写字母

- (BOOL)isLetter:(NSString *)letter {
    NSString *LETTER = @"[a-z]+";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", LETTER];
    return [regextestmobile evaluateWithObject:letter];
}

检验是否含有数字

- (BOOL)isNumber:(NSString *)number {
    NSString *NUMBER = @"[0-9]+";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", NUMBER];
    return [regextestmobile evaluateWithObject:number];
}

检验是否含有特殊字符

- (BOOL)isCharacter:(NSString *)character {
    NSString *CHARACTER = @"[^%&',;=?$\x22]+";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CHARACTER];  
    return [regextestmobile evaluateWithObject:character];
}

检验密码长度

- (BOOL)isPasswordLength:(NSString *)passwordLength {
    NSString *PASSWORD = @"^(?=.*[0-9].*)(?=.*[A-Z].*)(?=.*[a-z].*).{6,32}$";    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", PASSWORD];
    return [regextestmobile evaluateWithObject:passwordLength];
}

检验用户名

- (BOOL)isNickname:(NSString *)nickname {
    NSString *NICKNAME = @"^[a-zA-Z0-9\u4e00-\u9fa5][a-zA-Z0-9\u4e00-\u9fa5]{1,16}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", NICKNAME];
    return [regextestmobile evaluateWithObject:nickname];
}

上一篇下一篇

猜你喜欢

热点阅读