美文网首页
iOS手机号,邮箱,省份证验证

iOS手机号,邮箱,省份证验证

作者: gezhenrong | 来源:发表于2017-01-08 23:48 被阅读26次

    验证邮箱

    + (BOOL)isValidUsername:(NSString *)username {
        
        // 验证用户名 - 邮箱
        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:username];
    }
    

    验证手机号

    + (BOOL)isTelephone:(NSString *)mobile
    {
        if (mobile.length < 11)
        {
            return NO;
        }else{
            /**
             * 移动号段正则表达式
             */
            NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
            /**
             * 联通号段正则表达式
             */
            NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
            /**
             * 电信号段正则表达式
             */
            NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
            NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
            BOOL isMatch1 = [pred1 evaluateWithObject:mobile];
            NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
            BOOL isMatch2 = [pred2 evaluateWithObject:mobile];
            NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
            BOOL isMatch3 = [pred3 evaluateWithObject:mobile];
            
            if (isMatch1 || isMatch2 || isMatch3) {
                return YES;
    //            if (isMatch1) {
    //                return @"该号码是一个正确的移动手机号码";
    //            }else if (isMatch2) {
    //                return @"该号码是一个正确的联通手机号码";
    //            }else {
    //                return @"该号码是一个正确的电信手机号码";
    //            }
            }else{
                return NO;
            }
        }
        return nil;
    }
    

    验证身份证是否合法

    + (BOOL)validateIDCardNumber:(NSString *)valueStr{   
        valueStr = [valueStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];  
        NSUInteger length =0;    
        if (!valueStr) {        
            return NO;       
        }else {      
            length = valueStr.length;        
            if (length !=15 && length !=18) {            
                return NO;            
            }
        }    
        // 省份代码
        NSArray *areasArray =@[@"11",@"12", @"13",@"14", @"15",@"21", @"22",@"23", @"31",@"32", @"33",@"34", @"35",@"36", @"37",@"41",@"42",@"43", @"44",@"45", @"46",@"50", @"51",@"52", @"53",@"54", @"61",@"62", @"63",@"64", @"65",@"71", @"81",@"82", @"91"];   
        NSString *valueStart2 = [valueStr substringToIndex:2]; 
        BOOL areaFlag =NO;    
        for (NSString *areaCode in areasArray) {      
            if ([areaCode isEqualToString:valueStart2]) {           
                areaFlag =YES;          
                break;           
            }
        } 
        if (!areaFlag) {    
            return false;      
        }  
        NSRegularExpression *regularExpression;   
        NSUInteger numberofMatch; 
        int year =0;
        switch (length) {         
            case15:          
                year = [valueStr substringWithRange:NSMakeRange(6,2)].intValue +1900;          
                if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {               
                    regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$"                                                                                                            options:NSRegularExpressionCaseInsensitive                                                                                                              error:nil];//测试出生日期的合法性               
                }else {               
                    regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$"                                                                                                            options:NSRegularExpressionCaseInsensitive                                                                                                            error:nil];//测试出生日期的合法性
                }            
                numberofMatch = [regularExpression numberOfMatchesInString:valueStr                                                                                          options:NSMatchingReportProgress                                                                                             range:NSMakeRange(0, valueStr.length)];            
                if(numberofMatch >0) {                
                    return YES;                
                }else {               
                    return NO;               
                }            
            case18:
                year = [valueStr substringWithRange:NSMakeRange(6,4)].intValue;           
                if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {
                                regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$"
    options:NSRegularExpressionCaseInsensitive                                           error:nil];//测试出生日期的合法性                
                }else {
                    regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$"
    options:NSRegularExpressionCaseInsensitive
                                        error:nil];//测试出生日期的合法性                
                }
                numberofMatch = [regularExpression numberOfMatchesInString:valueStr
    options:NSMatchingReportProgress
                    range:NSMakeRange(0, valueStr.length)];
                if(numberofMatch >0) {
                    int S = ([valueStr substringWithRange:NSMakeRange(0,1)].intValue + [valueStr substringWithRange:NSMakeRange(10,1)].intValue) *7 + ([valueStr substringWithRange:NSMakeRange(1,1)].intValue + [valueStr substringWithRange:NSMakeRange(11,1)].intValue) *9 + ([valueStr substringWithRange:NSMakeRange(2,1)].intValue + [valueStr substringWithRange:NSMakeRange(12,1)].intValue) *10 + ([valueStr substringWithRange:NSMakeRange(3,1)].intValue + [valueStr substringWithRange:NSMakeRange(13,1)].intValue) *5 + ([valueStr substringWithRange:NSMakeRange(4,1)].intValue + [valueStr substringWithRange:NSMakeRange(14,1)].intValue) *8 + ([valueStr substringWithRange:NSMakeRange(5,1)].intValue + [valueStr substringWithRange:NSMakeRange(15,1)].intValue) *4 + ([valueStr substringWithRange:NSMakeRange(6,1)].intValue + [valueStr substringWithRange:NSMakeRange(16,1)].intValue) *2 + [valueStr substringWithRange:NSMakeRange(7,1)].intValue *1 + [valueStr substringWithRange:NSMakeRange(8,1)].intValue *6 + [valueStr substringWithRange:NSMakeRange(9,1)].intValue *3;
                    int Y = S %11;
                    NSString *M =@"F";                
                    NSString *JYM =@"10X98765432";
                    M = [JYM substringWithRange:NSMakeRange(Y,1)];// 判断校验位
                    if ([M isEqualToString:[valueStr substringWithRange:NSMakeRange(17,1)]]) {
                        return YES;// 检测ID的校验位
                    }else {
                        return NO;                    
                    }
                }else {               
                    return NO;   
                }
            default:    
                return false;
        }    
    }
    

    获取星期

    +(NSString *)dateTomorrow:(NSDate *)date
    {
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSInteger unitFlags = NSYearCalendarUnit |
        NSMonthCalendarUnit |
        NSDayCalendarUnit |
        NSWeekdayCalendarUnit |
        NSHourCalendarUnit |
        NSMinuteCalendarUnit |
        NSSecondCalendarUnit;
        NSDateComponents *comps = [calendar components:unitFlags fromDate:date];
        long month = [comps month];
        long day = [comps day];
        long hour = [comps hour];
        long minute = [comps minute];
        long weekday = [comps weekday];
        NSString *weekdayString = @"";
        switch (weekday) {
            case 1:
                weekdayString = @"周日";
                break;
            case 2:
                weekdayString = @"周一";
                break;
            case 3:
                weekdayString = @"周二";
                break;
            case 4:
                weekdayString = @"周三";
                break;
            case 5:
                weekdayString = @"周四";
                break;
            case 6:
                weekdayString = @"周五";
                break;
            case 7:
                weekdayString = @"周六";
                break;
            default:
                break;
        }
        
        NSString *dayStr = nil;
        if (day < 10) {
            dayStr = [NSString stringWithFormat:@"0%ld",day];
        }else{
            dayStr = [NSString stringWithFormat:@"%ld",day];
        }
        
        NSString *hourStr = nil;
        if (hour < 10) {
            hourStr = [NSString stringWithFormat:@"0%ld",hour];
        }else{
            hourStr = [NSString stringWithFormat:@"%ld",hour];
        }
        
        NSString *minuteStr = nil;
        if (minute < 10) {
            minuteStr = [NSString stringWithFormat:@"0%ld",minute];
        }else{
            minuteStr = [NSString stringWithFormat:@"%ld",minute];
        }
        
        return [NSString stringWithFormat:@"%ld/%@ %@:%@",month,dayStr,hourStr,minuteStr];
       
    }
    

    相关文章

      网友评论

          本文标题:iOS手机号,邮箱,省份证验证

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