Form value validation and onsubmit action : Form Validation « Form Control « JavaScript DHTML






Form value validation and onsubmit action

 

<html>
<head>
<title>Form Validation</title>
<script type="text/javascript">
function checkValid() {
    var cityField = document.forms[0]["city"];
    if (cityField.value != "Van") {
        var cityDiv = document.getElementById("citydiv");
        cityDiv.style.fontWeight = "bold";
        return false;
    } else {
        return true;
    }
}

</script>
</head>
<body>
<form name="formexample" id="formexample" action="#">
<div id="citydiv">City: <input id="city" name="city"></div>
<div><input id="submit" type="submit"></div>
</form>
<script type="text/javascript">
    function init() {
        document.forms[0].onsubmit = function() { return checkValid(this) };
    }
    window.onload = init;
</script>
</body>
</html>

   
  








Related examples in the same category

1.Form Validator: time, date email, phone number, age etc
2.Form Validate: is blank, is digit and is integer
3.Log in form validation
4.Log in email validation
5.Comprehensive form validation
6.Illegal sub string validation
7.Email form validation
8.Form Validator
9.Form valiation for empty data, file name
10.Email Address Validate
11.Time Validate
12.URL Validate
13.Use JavaScript to check the password input
14.Check form input value and alert user by change the control background