0

I'm using parse to store my data. I have a bunch of UITextField for user registration inside a view controller.

Now, in my phone text field, how do I format the text field to show the following depending on the total length. +55 (21) 99999-9999 = 13 numbers +55 (21) 9999-9999 = 12 numbers

I want to accept both 12 and 13 numbers and show the formatted phone in the textfield.

Now, for saving it to parse, I would like to save the formatted number with characters +, (, ), -.

I would also like to format my date text field to dd/mm/yyyy. Can anyone help me?

Thanks.

UPDATE Ok, so I did the following:

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

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

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

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

    return YES;
}

and

else if (textField == self.telefoneTextField)
    {
        if (_telefoneTextField.text.length == 14)
            {
                NSLog(@"Telefone sem o 9");
                [self.nascimentoTextField becomeFirstResponder];
            }
        if (_telefoneTextField.text.length == 15)
            {
                NSLog(@"Telefone COM o 9");
                NSMutableString *telefone = [[NSMutableString alloc]   initWithString:_telefoneTextField.text];
                [telefone deleteCharactersInRange:NSMakeRange(9,1)];
                [telefone insertString:@"-" atIndex:10];
                NSLog(@"%@", telefone);
                _telefoneTextField.text = telefone;
                [self.nascimentoTextField becomeFirstResponder];
            }
        else
        {
            NSLog(@"Telefone NAO esta no formato");
        }

    }

now, it works like I wanted. It changes the format in real time when the user is typing and when finish editing it checks to see how many chars, in case of 15, it changes the format again.

Now, one thing I couldn't do: How can I delete the phone number using the keyboard, I mean, it does't delete the numbers before the "-"character.

Thanks.

ferrojr
  • 339
  • 2
  • 4
  • 9
  • I don't quite get it.. What do you mean with 'i'm using parse'? Are you referring to a framework or specific method or are you 'parsing' NSString to number? I do this with NSRegularExpression to validate and then save as string. For the date field if you want to print a date in a specific format use NSDateFormatter class. – Peter Segerblom Dec 11 '13 at 12:34
  • u can try this for the phone number http://stackoverflow.com/a/13227608/2798777 – matthias Dec 11 '13 at 12:34

1 Answers1

0

The best way to implement this by using regular expressions. See the following discussion Regex not working correctly on iOS

Community
  • 1
  • 1
LML
  • 1,659
  • 12
  • 29