How to format UITextField for credit card input
So you are writing an app which has credit card entry form and you want to format the credit card numbers and expiry as xxxx-xxxx-xxxx-xxxx, xx/xx. How do you do that? Let’s first start with how can you pass your UITextField text to formatter (assume they exist for now, more on that in a while). Let’s say you have two UITextFields creditCardNumberTextField and creditCardExpiryTextField. The input in them needs to be formatted as an when any new input is done. So, setup triggers for that in viewDidLoad.
Let’s write the formatter now.
The above code will properly format credit card numbers and expiry dates as you enter them in the textfield. There is one more thing that is still remaining. You want the input to stop when the input textfield has all the credit card numbers inputted or has the expiry inputted. That can be achieved by the following code: