0

I am developing iPhone application. I need to take input from user in textfield in fix format like xx-xxx-xxx. So I am not getting how to do this? Please suggest me the solution.

iOSHack
  • 64
  • 1
  • 7

1 Answers1

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


if (range.location == 10)    //this is calculate the overall string
 {
        return NO;
    }

    if ([string length] == 0)   //string length is `0` hide the keyboard
    {
        return YES;
    }

    if ((range.location == 2) || (range.location == 6) )    //apply the `-` between the overall string
    {
        NSString *str  = [NSString stringWithFormat:@"%@-",textField.text];
        textField.text = str;
    }

}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143