iOS 验证特殊字符,身份证,邮箱,号码,用户名等

//1、数字字符校验


#define NUMBERSPERIOD @"0123456789."

+ (BOOL)checkNumber:(NSString *)string

{

    NSCharacterSet*cs;

    cs =[[NSCharacterSet characterSetWithCharactersInString:NUMBERSPERIOD]invertedSet];

    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];

    BOOL basicTest = [string isEqualToString:filtered];

    return basicTest;

}


//2、判断用户名,在216


+ (BOOL)checkName:(NSString *)_text

{

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

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

    return [Test evaluateWithObject:_text];

}



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

+ (BOOL) checkMobile:(NSString *)mobile

{

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

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

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

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

    return [phoneTest evaluateWithObject:mobile];

}



//4、判断邮箱


+ (BOOL)checkEmail:(NSString  *)_text

{

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

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

    return [Test evaluateWithObject:_text];

}


//5、判断密码,620


+ (BOOL)checkPassWord:(NSString  *)_text

{

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

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

//    return [Test evaluateWithObject:_text];

    NSLog(@"------   %d",[_text length]);

    if ([_text length] >= 6 && [_text length] <= 20) {

        return YES;

    }

    

    return NO;

    

}


//6. 身份证号,15  18


+ (BOOL)checkIDCardBaseFormate:(NSString  *)_text

{

    NSString *Regex = @"^(\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$" ;


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

    return [Test evaluateWithObject:_text];

}


+(BOOL) checkIDCard :(NSString*) idCard{

    int num = idCard.length;    //身份证位数

    Boolean num15 = num == 15;

    if(![self checkIDCardBaseFormate:idCard]){

        NSLog(@"不是身份证号");

        return NO;              //不是身份证号

    }

    else if(num15){

        NSMutableString *String =[[NSMutableString alloc] initWithString:idCard];

        [String insertString:@"19" atIndex:6];

        idCard = String;

    }

    

    NSString * address = @"11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91"//省份校验码

    

    NSRange range = [address rangeOfString:[idCard substringToIndex:2]];

    if(range.length == 0){

        NSLog(@"省份验证失败");

        return NO;              //没有匹配到省份

    }

    

    NSString *brithYear = [idCard substringWithRange:NSMakeRange(6, 2)];

    NSString *brithMonth = [idCard substringWithRange:NSMakeRange(10, 2)];

    NSString *brithDay = [idCard substringWithRange:NSMakeRange(12, 2)];

    

    if(![brithYear isEqualToString:@"19"] && ![brithYear isEqualToString:@"20"]){

        NSLog(@"生日年份验证失败");

        return NO;

    }

    if([brithMonth intValue] <1 || [brithMonth intValue] >12){

        NSLog(@"生日月份验证失败");

        return NO;

    }

    if([brithDay intValue] <1 || [brithDay intValue] >31){

        NSLog(@"生日日期验证失败");

        return NO;

    }

    

    NSArray *arrVarifyCode = [@"1,0,x,9,8,7,6,5,4,3,2" componentsSeparatedByString:@","];

    NSArray *wi = [@"7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2" componentsSeparatedByString:@","];

    int sum = 0;

    for(int i=0;i<17;i++){

        NSString *temp =[idCard substringWithRange:NSMakeRange(i,1)];

        sum += [wi[i] intValue] * [temp intValue];

    }

    int y = sum % 11;

    if(!num15){

        if(![arrVarifyCode[y] isEqualToString:[[idCard substringFromIndex:17] lowercaseString]])

        {

            NSLog(@"校验码验证失败");

            return NO;      //校验码验证失败

        }

    }

    NSLog(@"验证成功");

    return YES;

}



//判断固话    * 大陆地区固话及小灵通

//26         * 区号:010,020,021,022,023,024,025,027,028,029

//27         * 号码:七位或八位

+ (BOOL)checkConTel:(NSString *)_text

{

    NSString * Regex = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";

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

    return [Test evaluateWithObject:_text];

}


***关键方法




+(BOOL)isIncludeSpecialCharact: (NSString *)str {

    

    //***需要过滤的特殊字符:~#&*<>《》()[]{}【】^@/¤|§¨「」『』¢¬ ̄~@#&*()——+|《》$_€

    

    NSRange urgentRange = [str rangeOfCharacterFromSet: [NSCharacterSet characterSetWithCharactersInString: @"~#&*<>《》()[]{}【】^@/¤|§¨「」『』¢¬ ̄~@#&*()——+|《》$_€"]];

    

    if (urgentRange.location == NSNotFound)

        

    {

        

        return NO;

        

    }    

    

    return YES;

    

}


+ (BOOL)checkTureName:(NSString *)str

{

    if ([self isIncludeSpecialCharact:str]) {

        return NO;

    }

    if ([str length] > 10) {  

        return NO;

    }

    

    return YES;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值