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:
<html> <head> <title>JavaScript Validations</title> <script> function validateForm() { var x=document.forms["Form1"]["name"].value; if (x==null || x=="") { alert("name must be filled out"); return false; } }</script> </head> <body> <form name="Form1" action="second.php" onsubmit="return validateForm()" method="post"> name: <input type="text" name="name"> <input type="submit" value="Submit"> </form> </body> </html>