skip to Main Content

Html – form validation using javascript,if the enter values are not valid the input field has to show alert using css

//app.js let form2=document.getElementById("form"); let userName2=document.getElementById("userName"); form2.addEventListener("submit",(e)=>{ e.preventDefault(); validateInputs(); }) function validateInputs(){ let userNameVal=userName2.value.trim(); if(userNameVal === ""){ setError(userName2,"user name is required"); } else{ setSuccess(userName2); } } function setError(element,message){ let inputGroup=element.parentElement; let errorElement=inputGroup.querySelector("small"); errorElement.innerText=message; inputGroup.classList.add("error"); inputGroup.classList.remove("success"); } function setSuccess(element){ let inputGroup=element.parentElement; let…

VIEW QUESTION
Back To Top
Search