Wednesday, August 9, 2017

Form fileds validation in Javascript

Form fileds validation in JavaScript


Javascript function for validation:

function formValidation() {
    var filed = document.forms["loginForm"]["userName"].value;
    if (filed == "") {
        alert("Name should not be empty");
        return false;
    }
}



Loging page code:


<form name="loginForm" action="/message.jsp" onsubmit="return formValidation()" method="post">
  Name: <input type="text" name="userName">
  <input type="submit" value="Submit">
</form>

Automatic Validation :

<input type="text" name="fname" required>

No comments: