Itβs best to bet on the work of someone who has already done this for you, through a validation library and / or input masking library. Do not reinvent the wheel, a few good round ones, with rims suitable for almost any car, have already been invented for you.
Not linking specific here, because it's not exactly the point, a search in the "JavaScript form validation library" will find a lot.
Change I have not been looking for some time, I have to say that I like the look of this one . Both verification and masking, and not specific to the library (although there is a jQuery plugin available if you want). Again, snapping them is actually wrong, but she looked cool enough to shout out.
Tj crowder
source share