0

My problem is that I have some text field with number and I must to convert this number to some phone format like this one (xxx) xxx-xxxx. I have tried an regular expression with this code:

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);

If I gradually enter a text in text field, NSLog output this:

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

So my problem that I do not need brackets and hyphens if there is no number before it, i.e. closing bracket should appear after I enter 4th number and hyphen should appear after I enter 7th number.

user1248568
  • 621
  • 1
  • 9
  • 29

3 Answers3

0

use below code

- (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;
}

try this you will be succeed

Pratik
  • 2,399
  • 17
  • 36
0

Use this Utility

enter image description here

UITextField subclass that allows number input in a predefined format.

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

abdus.me
  • 1,819
  • 22
  • 34
0

If you have access to lazy operators, this will do what you want (I guess, you didn't give that much details.):

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

How? Lazy operators.

Loamhoof
  • 8,293
  • 27
  • 30