美文网首页
iOS 正则表达式判断输入的内容

iOS 正则表达式判断输入的内容

作者: BeeQiang | 来源:发表于2019-05-13 16:44 被阅读0次

//检查固定话

+ (BOOL)checkHomePhoneNumber:(NSString *) Number
{
//    @"^[0]\\d{2}-\\d{8}|^[0]\\d{3}-\\d{7}|^[0]\\d{2}\\d{8}|^[0]\\d{3}\\d{7}"
//    NSString *pattern = @"^[0]\\d{2}-\\d{8}|^[0]\\d{3}-\\d{7}|^[0]\\d{2}\\d{8}|^[0]\\d{3}\\d{7}|^[0]\\d{2}-\\d{9}|^[0]\\d{3}-\\d{8}|^[0]\\d{2}\\d{9}|^[0]\\d{3}\\d{8}";
    
    NSString *pattern = @"^(0[0-9]{2,3})?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:Number];
    return isMatch;
}

//6为数字密码

+ (BOOL)checkSixNumber:(NSString *) SixNumber
{
    NSString *pattern = @"^[0-9]{6}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:SixNumber];
    return isMatch;
}

//正则匹配手机号

+ (BOOL)checkTelNumber:(NSString *) telNumber
{
//    NSString *pattern = @"^1+[3578]+\\d{9}";
//    NSString *pattern = @"^1+[0123456789]+\\d{9}";
    
//    NSString *pattern = @"^[1][0123456789][0-9]{9}$";
    NSString *pattern = @"^1[3|4|5|7|8]+\\d{9}$";

//    ^1[3|4|5|7|8]\d{9}$
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:telNumber];
    return isMatch;
}

//正则匹配用户密码6-16位数字和字母组合

+ (BOOL)checkPassword:(NSString *) password
{
    NSString *pattern = @"[a-zA-Z0-9~`!@#$%^&*()-_=+\{}|:;\"'<>,./]{6,16}";
//    NSString *pattern = @"[a-zA-Z0-9]{6,16}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:password];
    return isMatch;
    
}

//正则匹配用户姓名,20位的中文或英文

+ (BOOL)checkUserName : (NSString *) userName
{
    NSString *pattern = @"^[a-zA-Z\u4E00-\u9FA5]{1,20}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:userName];
    return isMatch;
    
}

//正则匹配用户身份证号15或18位

+ (BOOL)checkUserIdCard: (NSString *) idCard
{
    //[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx]
    // ([0-9]{15}$)|([0-9]{17}([0-9]|X))|([0-9]{17}([0-9]|x))
    NSString *pattern = @"^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx])$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:idCard];
    return isMatch;
}

#pragma 正则匹配用户身份证号15或18位
+ (BOOL)isCorrect:(NSString *)IDNumber
{
    NSMutableArray *IDArray = [NSMutableArray array];
    for (int i = 0; i < 18; i++) {
        NSRange range = NSMakeRange(i, 1);
        NSString *subString = [IDNumber substringWithRange:range];
        [IDArray addObject:subString];
    }
    // 系数数组
    NSArray *coefficientArray = [NSArray arrayWithObjects:@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2", nil];
    // 余数数组
    NSArray *remainderArray = [NSArray arrayWithObjects:@"1", @"0", @"X", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2", nil];
    int sum = 0;
    for (int i = 0; i < 17; i++) {
        int coefficient = [coefficientArray[i] intValue];
        int ID = [IDArray[i] intValue];
        sum += coefficient * ID;
    }
    NSString *str = remainderArray[(sum % 11)];
    NSString *string = [IDNumber substringFromIndex:17];
    if ([str isEqualToString:string]) {
        return YES;
    } else {
        return NO;
    }
}

//正则匹员工号,12位的数字

+ (BOOL)checkEmployeeNumber : (NSString *) number
{
    NSString *pattern = @"^[0-9]{12}";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:number];
    return isMatch;
    
}

//正则匹配URL

+ (BOOL)checkURL : (NSString *) url
{
    NSString *pattern = @"^[0-9A-Za-z]{1,50}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:url];
    return isMatch;
    
}

// 匹配正浮点数和整数

+ (BOOL)checkFloat : (NSString *) floatStr
{
    NSString *pattern = @"^[0-9]+([.]{0,1}[0-9]+){0,1}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:floatStr];
    return isMatch;
}

//匹配正整数

+ (BOOL)checkunsignedInt: (NSString *) floatStr
{
    NSString *pattern = @"^[0-9]{0,10}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:floatStr];
    return isMatch;
}

// 匹配中文

+ (BOOL)checkChinese:(NSString *)chinese
{
    NSString *pattern = @"^[\\u2E80-\\uFE4F]+$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:chinese];
    return isMatch;
}

//匹配金钱

+ (BOOL)checkMoney:(NSString *)money
{
    NSString *pattern = @"^(?!0+(?:\\.0+)?$)(?:[1-9]\\d*|0)(?:\\.\\d{1,2})?$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:money];
    return isMatch;
}

// 匹配邮箱

+ (BOOL)isValidateEmail:(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];
}

相关文章

  • UITextField输入限制汇总

    iOS常用正则表达式 textField输入金额、数字、整数、小数 输入判断 2.textField输入个数限制遇...

  • iOS 正则表达式判断输入的内容

    //检查固定话 //6为数字密码 //正则匹配手机号 //正则匹配用户密码6-16位数字和字母组合 //正则匹配用...

  • isNaN()函数:检查参数是否是数字值

    在前端页面表单中,经常需要判断输入的内容是否合法。比如要判断表单输入的是否为合法数字,很难用正则表达式或者表单属性...

  • iOS 密码框判断密码0~9数字和字母组合

    iOS textField输入密码时需要实时判断输入的内容的类型,反馈给用户只能输入规定的数字或字母组合,比如只能...

  • iOS 输入密码判断密码强度

    iOS 输入密码判断密码强度

  • 正则表达式

    为什么使用正则表达式: 思考:假如自己做了一个登录界面,怎么判断用户输入的一个邮箱内容是正确的邮箱格式,怎么判断用...

  • 2018-10-23day18总结

    一、正则表达式语法 正则表达式是用匹配或者描述字符串的工具。 用处: a.判断字符串是否满足某个条件---判断输入...

  • Day18-正则表达式

    一、正则表达式语法 正则表达式是用匹配或者描述字符串的工具。 用处: a.判断字符串是否满足某个条件---判断输入...

  • day18-正则表达式

    一、正则表达式语法 正则表达式是用匹配或者描述字符串的工具。 用处: a.判断字符串是否满足某个条件---判断输入...

  • Day17-正则表达式

    一、正则表达式语法 正则表达式是用匹配或者描述字符串的工具。 用处: a.判断字符串是否满足某个条件---判断输入...

网友评论

      本文标题:iOS 正则表达式判断输入的内容

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