-2

I'm trying to create a credit card processing view controller. I have a expiration date field that I would like to format as MM/YY on the fly.

I tried to follow the snippet from here UITextField format in xx-xx-xxx, but it uses NSNumber when used on a expiration date that has a leading 0 it doesn't work. I searched google and nothing came up either. The leading zero is removed. How can I use a UITextField to format as MM/YY on the fly as the user is inputing (ex. 05/16)?

It is possible because Stripe's UITextField for expiration date does do it in their library.

enter image description here

Community
  • 1
  • 1
Marquisk2
  • 159
  • 1
  • 15
  • 1
    How is removing the leading zero an issue? Can't you just format the text field using `[NSString stringWithFormat:@"%02d/%02d", month, year];`? – trojanfoe Nov 10 '15 at 18:14
  • He wants it formatted as 09/01 @trojanfoe And I was about to suggest using what trojanfoe wrote :D – Lefteris Nov 10 '15 at 18:19
  • it's easy. Just made textfield for number input and check whether entered number is less than 10 then add zero else enter value in textfield. – Gagan_iOS Nov 10 '15 at 18:19
  • Update your question with what you tried and explain what issues you are having with the code. – rmaddy Nov 10 '15 at 18:23
  • But how can you format the string as the user is entering the information into the TextField? – Marquisk2 Nov 10 '15 at 18:23
  • Have two separate fields; one for month and one for year with `/` in between as a `UILabel` and then use [this](http://stackoverflow.com/questions/11131457/nsnumberformatter-add-extra-zero)? – trojanfoe Nov 10 '15 at 18:25
  • I could use two separate fields, but it's also possible to have one which is what I'm looking for. Stripe's API does it so it's possible! – Marquisk2 Nov 11 '15 at 14:00

1 Answers1

4

I'm not sure why I got down voted for a legitimate question, but I solved my problem so others can avoid asking it.

Your class needs to conform to the UITextFieldDelegate in order for this to work.

I created an action based on the UIControlEventEditingChanged event in the viewDidLoad function.

     [dateUIText addTarget:self
             action:@selector(dateTextFieldDidChange:) 
             forControlEvents:UIControlEventEditingChanged];

This is what the dateTextFieldDidChange function looks like when adding the "/" after the month:

- (void) dateTextFieldDidChange: (UITextField *)theTextField {

   NSString *string = theTextField.text;

   if (string.length == 2) {

       theTextField.text = [string stringByAppendingString:@"/"];

   } else if (string.length > 5) {

       theTextField.text = [string substringToIndex:5];

   }

}

When the user tries to backspace to make any adjustments to the month the function above won't allow it so this function is then needed to assist:

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


   if ([string isEqualToString:@""] && textField.text.length == 3) {

       NSString *dateString = textField.text;
         textField.text = 
            [dateString stringByReplacingOccurrencesOfString:@"/" withString:@""];

}


   return YES;
}
Marquisk2
  • 159
  • 1
  • 15