JavaScript Form Validation

What is JavaScript Validation? JavaScript is use to validate form data in HTML documents before send to server. Check the required field is missing or not, check the email address is valid or not etc.

JavaScript Form Validation

Syntax

function form_validate() {
  var value; // variable declaration assign form value
  if (value==null || value=="") {
    alert(alert message);
    return false;
  } else {
    return true;
  }
}

Logic Condition

function form_validate() {
  var val=document.forms["check_form"]["ur_name"].value
  if (val==null || val=="") {
    alert("Name must be Required..!");
    return false;
  }
}

Example

<!DOCTYPE html>
<html lang="en">
<head>
  <title>JavaScript Form Validation</title>
</head>
<body>
    <script type="text/javascript">
    function form_validate() {
        var val=document.forms["check_form"]["ur_name"].value;
        if (val==null || val=="") {
            alert("Name must be required..!");
            return false;
        } else {
            alert("Successful Validate...");
        }  
    }
    </script>

    <form name="check_form" action="form_submit_message.php" method="post" onSubmit="return form_validate()">
        Enter Your Full Name: <input type="text" name="ur_name" size="35"/>
        <input type="submit" value="Submit"/>
    </form>
</body>
</html>

Run it...   »

Example Result