Objective:
To ensure that phone numbers are entered in the correct format (e.g. 208-111-1111) and display an error message if the format is incorrect. How can we make sure the error message disappears once the user re-enters the correct format?
JS
phoneNumber = document.getElementById("phone");
var result = phoneNumber.toString().match(/^\d{3}-\d{3}-\d{4}$/);
function validatePhone(){
if (result == null)
{
error2.innerHTML = "The number is not in a correct format";
} else
{
error2.innerHTML = " ";
};
}
HTML
<p>Phone:</p>
<input type = "text" id="phone" name="phone" onChange="validatePhone()">
<br>
<span id="error2" ></span>