0

我的问题是我有一些带有数字的文本字段,我必须将此数字转换为某种电话格式,例如(xxx) xxx-xxxx。我已经用这段代码尝试了一个正则表达式:

wholeText = [wholeText stringByReplacingOccurrencesOfString:@"(\\d{1,3})(\\d{0,3})(\\d{0,4})"
                                                     withString:@"($1) $2-$3"
                                                        options:NSRegularExpressionSearch
                                                          range:NSMakeRange(0, wholeText.length)];
NSLog(@"wholeText = %@", wholeText);

如果我逐渐在文本字段中输入文本,NSLog输出以下内容:

wholeText = (1) -
wholeText = (12) -
wholeText = (123) -
wholeText = (123) 4-
wholeText = (123) 45-
wholeText = (123) 456-
wholeText = (123) 456-7

所以我的问题是如果前面没有数字,我不需要括号和连字符,即在我输入第 4 个数字后应该出现右括号,在我输入第 7 个数字后应该出现连字符。

4

3 回答 3

0

使用下面的代码

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

    int length = [self getLength:textField.text];
    //NSLog(@"Length  =  %d ",length);

    if(length == 10)
    {
        if(range.length == 0)
            return NO;
    }

    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString: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:textField.text];
        textField.text = [NSString stringWithFormat:@"(%@)",num];
        if(range.length > 0)
            textField.text = [NSString stringWithFormat:@"%@",[num substringToIndex:3]];
    }
    else if(length == 6)
    {
        NSString *num = [self formatNumber:textField.text];
        //NSLog(@"%@",[num  substringToIndex:3]);
        //NSLog(@"%@",[num substringFromIndex:3]);
        textField.text = [NSString stringWithFormat:@"(%@) %@-",[num  substringToIndex:3],[num substringFromIndex:3]];
        if(range.length > 0)
            textField.text = [NSString stringWithFormat:@"(%@) %@",[num substringToIndex:3],[num substringFromIndex:3]];
    }
    return YES;
}

#pragma mark - Mobile Validation

-(NSString*)formatNumber:(NSString*)mobileNumber
{

    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];

    NSLog(@"%@", mobileNumber);

    int length = [mobileNumber length];
    if(length > 10)
    {
        mobileNumber = [mobileNumber substringFromIndex: length-10];
        NSLog(@"%@", mobileNumber);

    }


    return mobileNumber;
}


-(int)getLength:(NSString*)mobileNumber
{

     mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
     mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
     mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
     mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
     mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];

     int length = [mobileNumber length];

     return length;
}

试试这个你会成功的

于 2013-04-01T09:38:03.903 回答
0

使用此实用程序

在此处输入图像描述

UITextField 子类,允许以预定义格式输入数字。

http://www.cocoacontrols.com/controls/reformattednumberfield

于 2013-04-01T09:40:24.717 回答
0

如果您可以访问惰性运算符,这将满足您的需求(我猜,您没有提供太多细节。):

/^(\d{1,3}?)(\d{1,3}?)(\d{1,4})$/

如何?懒惰的运营商。

于 2013-04-01T09:40:27.450 回答