Below is the code snippet:
var nameInput = formHandle.f_Name;
var idInput = formHandle.f_Id;
// VALIDATING NAME
if(nameInput.value === ""){
nameMsg = document.getElementById("nameErr");
nameMsg.style.background = "red";
nameMsg.innerHTML = "Please enter your name.";
nameMsg.style.color = "white";
nameInput.focus();
return false;
}
// VALIDATING ID
if(idInput.value === ""){
idMsg = document.getElementById("idErr");
idMsg.style.background = "red";
idMsg.innerHTML = "Please enter the correct ID.";
idMsg.style.color = "white";
idInput.focus();
return false;
}
The validation process begins with checking the name field. If empty, a red alert appears. However, upon entering a name and submitting again, an empty ID field triggers an error. To ensure that only the ID field displays an error when left blank, how can this be done?