陈斌彬的技术博客

Stay foolish,stay hungry

iOS 正则表达式实例

判断 账号 2-16位

+(BOOL)checkAccountInput:(NSString *)_text
{

    NSString *Regex = @"^\\w{2,16}$";

    NSPredicate *tPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];

    if ([tPredicate evaluateWithObject:_text]) {
        return YES ;
    }

    return NO ;

}

判断手机号码,1开头的十一位数字

+(BOOL)checkPhoneNumInput:(NSString *)_text
{

    NSString *Regex = @"1\\d{10}";

    NSPredicate *tPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];

    if ([tPredicate evaluateWithObject:_text]) {
        return YES ;
    }

    return [tPredicate evaluateWithObject:_text];

}

判断邮箱格式

+(BOOL)checkMailInput:(NSString *)_text
{

    NSString *Regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";

    NSPredicate *tPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];

    if ([tPredicate evaluateWithObject:_text]) {
        return YES ;
    }

    return NO ;

}

判断密码 6-16位

+(BOOL)checkPasswordInput:(NSString *)_text
{

    NSString *Regex = @"\\w{6,16}";

    NSPredicate *tPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];

    if ([tPredicate evaluateWithObject:_text]) {
        return YES ;
    }

    return NO;

}

判断验证码 6位

+(BOOL)checkCodeInput:(NSString *)_text
{

    NSString *Regex = @"\\w{6}";

    NSPredicate *tPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];

    if ([tPredicate evaluateWithObject:_text]) {
        return YES ;
    }

    return NO;

}

判断身份证 18位

+(BOOL)checkCerticateInput:(NSString *)_text
{

    NSString *Regex = @"\\w{18}";

    NSPredicate *tPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];

    if ([tPredicate evaluateWithObject:_text]) {
        return YES ;
    }

    return NO;

}