Required Fields JavaScript Validation

The function below checks if a field has been left empty. If the field is blank, an alert box alerts a message, the function returns false, and the form will not be submitted:

<title>JavaScript Validations</title>
function validateForm()
var x=document.forms["Form1"]["name"].value;
if (x==null || x=="")
  alert("name must be filled out");
  return false;
<form name="Form1" action="second.php" onsubmit="return validateForm()" 
  <input type="text" name="name">
  <input type="submit" value="Submit">

You may also like...