How can you format a form input field such that it only accepts numbers from users, takes a maximum of 16 digits, and adds a space after each four digits? I am trying to create a debit card such that the data a user enters in the form input fields are updated in real time on the card displayed by the side. And I want the card number to be arranged in fours, just as it is on a real card.
I tried saving the user input in a variable and then using a loop to insert a space after every four digits. Well, I don’t know why but it didn’t work.
2
Answers
I made a simple example using listeners and regular expressions.
You can add an event listener that uses regex that modifies the number input by the user:
JavaScript
Minimal HTML page: