shouldChangeCharactersInRange对于不同的文本字段无法正常工作

时间:2014-07-31 18:44:23

标签: ios regex xcode uitextfield

我正在处理电话号码正则表达式,如XXX XXX-XXXX的电话文字字段,代码工作正常,但它会打扰其他文本字段,如姓名,电子邮件,我应该怎么做才能将名称正则表达式与nameTextFields分开 我写了这样的代码

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

    if(textField==txtName){
        if (txtName.text.length >2) {
            tickFirstName.hidden = NO;
           [tickFirstName setTextColor:  [UIColor colorWithRed:56.0/255.0 green:234.0/255.0 blue:126.0/255.0 alpha:1]];

    }
        if (textField.text && textField.text.length == 0)
        {
             tickFirstName.hidden = YES;
        }
        else
        {
            /* what ever */
        }
}

    if([textField == txtPhone)
        {

            if ([txtPhone.text length]>0) {



        int length = [txtPhone.text length];
        NSLog(@"Length  =  %d ",length);
        if([txtPhone.text length] == 12)
        {
            if(range.length == 0)
                return NO;
        }
        NSString *newString = [txtPhone.text stringByReplacingCharactersInRange:range withString:string];
                NSLog(@"-------------%@",string);
        NSCharacterSet *charactersToRemove = [[ NSCharacterSet alphanumericCharacterSet ] invertedSet ];
        newString = [[newString componentsSeparatedByCharactersInSet:charactersToRemove]componentsJoinedByString:@""];
        NSString *expression = @"^([0-9]+)?(\\.([0-9]{1,2})?)?$";
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression
                                                                               options:NSRegularExpressionCaseInsensitive
                                                                                 error:nil];
        NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString
                                                            options:0
                                                              range:NSMakeRange(0, [newString length])];
        NSLog(@"newString::%@",newString);
        if (numberOfMatches == 0)
            return NO;
        if(length == 3)
        {
            NSString *num = [self formatNumber:txtPhone.text];
            txtPhone.text = [NSString stringWithFormat:@"%@",num];
            if(range.length > 0)
                txtPhone.text = [NSString stringWithFormat:@"%@",[num substringToIndex:3]];
        }
        else if(length == 6)
        {
            NSString *num = [self formatNumber:txtPhone.text];
            //NSLog(@"%@",[num  substringToIndex:3]);
            //NSLog(@"%@",[num substringFromIndex:3]);
            txtPhone.text = [NSString stringWithFormat:@"%@ %@-",[num  substringToIndex:3],[num substringFromIndex:3]];
            if(range.length > 0)
                txtPhone.text = [NSString stringWithFormat:@"%@ %@",[num substringToIndex:3],[num substringFromIndex:3]];
        }

    }
    else
    {
        NSLog(@"333");

    }
        }
    return YES;
}

0 个答案:

没有答案