This page describes the JavaScript usages.



Conditional Validations

You can conditionally validate a form by calling a function with a return value. Say for example, let's have a function named validate() declared as follows:

/* assume this function validates the min length of 
   'password' for not being less than 8 characters */
function validate()
   var canSubmit = "true";
   var pwd = document.getElementById("passwd").value;
   if(pwd.length < 8)
       alert ("password can't be less than 8 characters..");
       canSubmit = "false";
    return false;

The same function should be called with return keyword in the calling place. Say for example we call the validate() function in the onclick of the Submit button as follows

<form name="MyForm" submit="SubmitForm.html">
    <input type="text" id="passwd" size="10" value="pwd">
    <input type="submit" value="submit" onclick="return validate()"/>

Generally, the validations can happen by invoking a javascript function on any of the event related methods like (onclick, onfocus, onchange etc.,) but you will not really get a control over the form submission. No matter what the validation succeeds or fails, the form will get submitted and it would be like the form validation does not have any effect at all :)

For which you need to invoke the method with 'return' and based on the return value (true or false), the real submission is decided.

Reference URL:

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.