IOS 正则表达式验证

转载自 广告比较多的网站

一、对邮箱进行校验

+ (BOOL)checkEmail:(NSString *)email
{
    NSString *emailReg = @"^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",emailReg];
    if ([regextestmobile evaluateWithObject:email] == YES){
        return YES;
    }
    return NO;
}

二、对身份证号进行校验

NSString *person = @"(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])";

三、对固话进行校验

eg:010-88776655

NSString *mobile = @"0\\d{2,3}-\\d{5,9}|0\\d{2,3}-\\d{5,9}";

eg:01088776655

NSString *mobile = @"0\\d{2,3}\\d{5,9}|0\\d{2,3}-\\d{5,9}";

四、对手机号进行校验

#pragma mark 检测手机号码是否正确
+ (BOOL)checkMobileNumber:(NSString *)mobileNum{
    mobileNum = [mobileNum stringByReplacingOccurrencesOfString:@"-" withString:@""];
    mobileNum = [mobileNum stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([mobileNum hasPrefix:@"00"])
        return NO;
    
    if ([mobileNum hasPrefix:@" "]){
        mobileNum = [mobileNum stringByReplacingOccurrencesOfString:@" " withString:@""];
    }
    
    /**
     * 手机号码
     * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通:130,131,132,152,155,156,185,186
     * 电信:133,1349,153,180,189
     *
     * 新增
     * 移动:152 154 178 183 184
     * 联通:176
     * 电信:177 181
     */
    
    NSString * MOBILE = @"^1(3[0-9]|4[0-9]|5[0-9]|7[67]|8[0-9])\\d{8}$";
    /**
     10         * 中国移动:China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,152,154,157,158,159,178,182,183,184,187,188
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[0-247-9]|7[8]|8[2-478])\\d)\\d{7}$";
    /**
     15         * 中国联通:China Unicom
     16         * 130,131,132,152,155,156,176,185,186
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|7[6]|8[56])\\d{8}$";
    /**
     20         * 中国电信:China Telecom
     21         * 133,1349,153,177,180,181,189
     22         */
    NSString * CT = @"^1((33|53|7[7]|8[019])[0-9]|349)\\d{7}$";
    /**
     25         * 大陆地区固话及小灵通
     26         * 区号:010,020,021,022,023,024,025,027,028,029
     27         * 号码:七位或八位
     28         */
    
    NSString *telNum = @"^0\\d[1-9]{1}(\\d[0-9]{1,2}\\d[1-9]{1})\\d{4,6}";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    NSPredicate *regextestTel = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", telNum];
    if ([regextestTel evaluateWithObject:mobileNum]){
        MyLog(@"pass");
    }
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES)
        || ([regextestTel evaluateWithObject:mobileNum] == YES)){
        return YES;
    }
    return NO;
}

五、纯数字

NSString *reg = @"^\\d*$";

六、纯字母

NSString *reg = @"^[A-Za-z]*$";

七、首字母为字母,其它为A-Za-z_0-9

NSString *reg = @"^[a-zA-Z]\\w{1,}$";

你可能感兴趣的:(IOS 正则表达式验证)