控制输入电话号码

//控制输入电话号码
+ (BOOL)validateNumber:(NSString*)number range:(NSRange)range{
    BOOL res = YES;
    NSCharacterSet *tmpSet;
    if (range.location == 0) {
        tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"1"];
    }
    else if(range.location == 1)
    {
        tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"34578"];
    }
    else
    {
        tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    }
    
    
    int i = 0;
    while (i < number.length)  {
        
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange tmpRange = [string rangeOfCharacterFromSet:tmpSet];
        if (tmpRange.length == 0) {
            res = NO;
            break;
        }
        else
        {
            if (range.location >= 11) {
                res = NO;
            }
            else
            {
                res = YES;
            }
        }
        i++;
    }
    return res;
}

 

posted @ 2017-01-11 14:42  超神船长  阅读(250)  评论(0编辑  收藏  举报