I am currently working on a simple form to practice Javascript validation and am having difficulty in adding the dashes in-between the numbers (like this: 123-45-6789). I need to keep the validation that displays whether or not it is valid (9 digits only) as the user is typing. This is what I have so far
function ssnFunction()
{
let x = document.getElementById("ssn").value;
let text;
if (isNaN(x) || x.toString().length != 9)
{
text = "\tMust be 9 digits";
ssnValidation = 0;
}
else
{
text = "Valid";
ssnValidation = 1;
}
document.getElementById("ssnvalid").innerHTML = text;
return ssnValidation;
}
<label for="ssn">Social Security: </label>
<input type="text"
name="ssn"
id="ssn"
value=""
required="required"
onkeyup="ssnFunction()" size="20" />
<span id="ssnvalid"></span>
Any help and explanation is greatly appreciated