Alex
Alex

Reputation: 229

Setting up phone number format while typing

I am setting phone number format such as (222) 233-3441 while typing in textField. When user keeps entering more that 14 characters (including special characters), all special characters will be removed and numbers alone be displayed (i.e. 222233344188). And when they reach back to 14 characters on deleting some characters, the phone number format will be set again. I achieved as i wanted. But facing problem while deleting.

Became blank to move forward. Please push me up with your valuable suggestions to resolve this issue.

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


    if(string.length!=0){   //detect backspace

        if (textField.text.length == 0)
            textField.text = [NSString stringWithFormat:@"(%@",textField.text];

        if (textField.text.length == 4)
            textField.text = [NSString stringWithFormat:@"%@) ",textField.text];

        if (textField.text.length == 9)
            textField.text = [NSString stringWithFormat:@"%@-",textField.text];

        if (textField.text.length>13){

            NSString *value=[NSString stringWithString:textField.text];
            textField.text=[[value componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]componentsJoinedByString:@""];

        }
    }
    else{

        if(textField.text.length==11){

            NSMutableString *text=[NSMutableString stringWithString:textField.text];
            [text insertString:@"(" atIndex:0];
            [text insertString:@") " atIndex:4];
            [text insertString:@"-" atIndex:9];
            textField.text=text;
        }

     }

     return YES;
}

Thanks.

Upvotes: 2

Views: 3583

Answers (3)

mixel
mixel

Reputation: 25836

I suggest more compact solution:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    BOOL result = YES;
    if (string.length != 0) {
        NSMutableString *text = [NSMutableString stringWithString:[[textField.text componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""]];
        [text insertString:@"(" atIndex:0];

        if (text.length > 3)
            [text insertString:@") " atIndex:4];

        if (text.length > 8)
            [text insertString:@"-" atIndex:9];

        if (text.length > 13) {
            text = [NSMutableString stringWithString:[text substringToIndex:14]];
            result = NO;
        }
        textField.text = text;
    }

    return result;
}

Upvotes: 4

mandeep
mandeep

Reputation: 384

Have a look at this code this might help u a bit

txtlpmobile.text is the string(Mobile no ur gonna enter)

 int length = [self getLength:txtLpMobile.text];
            if(length == 10) {
                if(range.length == 0)
                    return NO;
            }
            if(length == 3){
                NSString *num = [self formatNumber:txtLpMobile.text];
                txtLpMobile.text = [NSString stringWithFormat:@"(%@) ",num];

                if(range.length > 0) {
                    txtLpMobile.text = [NSString stringWithFormat:@"%@",[num substringToIndex:3]];

                }
            } else if(length == 6) {
                NSString *num = [self formatNumber:txtLpMobile.text];
                txtLpMobile.text = [NSString stringWithFormat:@"(%@) %@-",[num  substringToIndex:3],[num substringFromIndex:3]];
                if(range.length > 0) {
                    txtLpMobile.text = [NSString stringWithFormat:@"(%@) %@",[num substringToIndex:3],[num substringFromIndex:3]];
                }
            }

            NSUInteger newLength;
            newLength = [txtLpMobile.text length] + [string length] - range.length;
            NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS_ONLY] invertedSet];
            NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
            return (([string isEqualToString:filtered])&&(newLength <= CHARACTER_LIMIT));

for formatting number

-(NSString*)formatNumber:(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];
    if(length > 10)
    {
        mobileNumber = [mobileNumber substringFromIndex: length-10];
    }
    return mobileNumber;
}

for getting length

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

Upvotes: 0

Mundi
Mundi

Reputation: 80265

I agree with the comments, i.e. this is not advisable design.

Still, the programming question is interesting. My approach would be to first "normalize" the text, i.e. remove all non-numeric characters and then apply your logic.

textField.text=[[textField.text componentsSeparatedByCharactersInSet:
     [[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
     componentsJoinedByString:@""];

Upvotes: 1

Related Questions