I want to do a validate check to see if the SSN entered into a textbox follows this specific format: XXX-XX-XXXX
as its being typed
I tried doing a function that just auto formats like this...
function ssnCheck() {
var val = this.value.replace(/\D/g, '');
var newVal = '';
if(val.length > 4) {
this.value = val;
}
if((val.length > 3) && (val.length < 6)) {
newVal += val.substr(0, 3) + '-';
val = val.substr(3);
}
if (val.length > 5) {
newVal += val.substr(0, 3) + '-';
newVal += val.substr(3, 2) + '-';
val = val.substr(5);
}
newVal += val;
this.value = newVal;
}
But it would bug out sometimes and not fully work especially on Mobile devices. I tried the solutions below, but they aren't working. The way I'm applying the above is by doing this..
document.getElementById("ssn").addEventListener('keyup',ssnCheck,false);
Any help on getting it to work so it auto formats as its being typed, or a better solution to this issue since it HAS to show in the XXX-XX-XXXX format when they are done typing in the box.