美文网首页
iOS 一些常用的正则判断

iOS 一些常用的正则判断

作者: fuyang | 来源:发表于2019-08-06 10:04 被阅读0次

    1.匹配ip地址:NSString*regex =@"(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])";

    NSString*port = [self.InputTextField.textstringByMatching:regex];

    2.//邮箱

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

    }

    3.//手机号码验证

    - (BOOL)validateMobile{

    NSString *MOBILE = @"^1[34578]\\d{9}$";

    NSPredicate *regexTestMobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",MOBILE];

    if ([regexTestMobile evaluateWithObject:self]) {

    return YES;

    }else {

    return NO;

    }

    4.//车牌号验证

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

    }

    5.//车型

    + (BOOL) validateCarType:(NSString *)CarType

    {

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

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

    return [carTest evaluateWithObject:CarType];

    }

    6.//用户名

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

    }

    7.//密码

    + (BOOL) validatePassword:(NSString *)passWord

    {

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

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

    return [passWordPredicate evaluateWithObject:passWord];

    }

    8.//昵称

    + (BOOL) validateNickname:(NSString *)nickname

    {

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

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

    return [passWordPredicate evaluateWithObject:nickname];

    }

    9.//身份证号

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

    }

    10.//判断是否有特殊符号

    - (BOOL)effectivePassword

    {

    //    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"&\"< >\\/*/null & /* /NULL"];

    //    NSString *trimmedString = [self stringByTrimmingCharactersInSet:set];

    //    return trimmedString;

    NSString *regex = @"[a-zA-Z0-9]{6,20}";

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];

    return [predicate evaluateWithObject:self];

    }

    相关文章

      网友评论

          本文标题:iOS 一些常用的正则判断

          本文链接:https://www.haomeiwen.com/subject/cwlrlttx.html