正则验证手机号,邮箱,车牌

来源:互联网 发布:日语汉字读音软件 编辑:程序博客网 时间:2024/04/30 21:02

- (IBAction)button_Click:(id)sender {

    if ([selfisValidateMobile:@"输入你要验证的手机号码"] ==YES) {

        NSLog(@"手机号正确");

    }else{

        NSLog(@"手机号错误");

    }

    

    if ([selfisValidateEmail:@"输入你要验证的邮箱"] ==YES) {

        NSLog(@"邮箱正确");

    }else{

        NSLog(@"邮箱错误");

    }

    

   

    

}

/*手机号码验证 MODIFIED BY HELENSONG*/

-(BOOL) isValidateMobile:(NSString *)mobile

{

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

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

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

        //NSLog(@"phoneTest is %@",phoneTest);

    return [phoneTestevaluateWithObject:mobile];

}


/*车牌号验证 MODIFIED BY HELENSONG*/

BOOL validateCarNo(NSString* carNo)

{

    NSString *carRegex =@"^[A-Za-z]{1}[A-Za-z_0-9]{5}$";

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

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

    return [carTestevaluateWithObject:carNo];

}


/*邮箱验证 MODIFIED BY HELENSONG*/

-(BOOL)isValidateEmail:(NSString *)email

{

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

    NSPredicate *emailTest = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", emailRegex];

    return [emailTestevaluateWithObject:email];

}

0 0