How can I validate the form before submitting the form using Javascript?
How to validate form after submitting it.
Add Comment
Form
<form name="empid" id="empid" action="process.php" onsubmit="return formValidation()" method="post"> Name: <input type="text" name="fullname"> <input type="submit" value="Submit"> </form>
Using Javascript
For javascript you can see the attribute onsubmit=”return formValidation()” in the form. The function in this attributes return boolean value , you can see the function definition below. If the boolean value is true form will submit else form will not submit. So you can write any validation in this function, if this function returns true value then only form executes..
Function definition:
function formValidation()
{
var y=document.forms["empid"]["fullname"].value;
if (y==null || y=="")
{
alert("Full name must be filled out");
return false;
}
}