简体   繁体   中英

multiple function not working for onsubmit

My form is validating the two java functions script and if both are true it will continue the form submit. But when i submit it validates only first function validateFormROLLNO() not the second function. Also, when the first function fails it submits the form anyway. I want to submit the form only when the two functions are passed.

first function will check if the roll no = 12 characters. second function checks if the name is not null.

   <body>
   <center>
 <script type="text/javascript">


function validateFormROLLNO() {
    var x = document.forms["myForm"]["id"].value;
            if (x !=12) {
        alert("ROLLNO must be 12 characters long!!!!");
                        return false;
    }
document.forms["myForm"]["submit"].disabled = true;
document.forms["myForm"]["submit"].value="Wait..";
}


 function validateFormNAME() {
    var p = document.forms["myForm"]["student"].value;
            if (p =='') {
        alert("Name cannot be NULL!!!!");
                        return false;
    }
document.forms["myForm"]["submit"].disabled = true;
document.forms["myForm"]["submit"].value="Wait..";

}

function validate(){
return validateFormROLLNO() && validateFormNAME();
}

</script>

     <br> <FORM name="myForm"  ACTION="insert.jsp"   onsubmit="return validate()"  METHOD="POST">
        Please enter the Rollno and Name you want to INSERT:
        <BR> <br>
        <b>ISIN :<INPUT  TYPE="TEXT" NAME="id"></b>
        <BR><BR>
        <b>   SOURCE :<INPUT TYPE="TEXT" NAME="student"></b>
        <br><BR>

 <INPUT TYPE="SUBMIT" value="Submit">
    </FORM>
     </center>
</body>
</html>

There might be some javascript errors exists on the page, that don't let second function to execute. Please use browser's inspect element to explore it.

document.forms["myForm"]["id"].value you are are checking value of the textbox. Please document.forms["myForm"]["id"].value.length to validate it.

Here is the complete method.

function validateFormROLLNO() {
     var x = document.forms["myForm"]["id"].value;
        if (x.length <12) {
    alert("ROLLNO must be 12 characters long!!!!");
                    return false;
  }
  else
   return true;
 document.forms["myForm"]["submit"].disabled = true;
   document.forms["myForm"]["submit"].value="Wait..";
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM