简体   繁体   English

多重功能不适用于onsubmit

[英]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. 我的表单正在验证两个Java函数脚本,如果两个都正确,它将继续表单提交。 But when i submit it validates only first function validateFormROLLNO() not the second function. 但是,当我提交时,它仅验证第一个函数validateFormROLLNO()而不验证第二个函数。 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. 第一个功能将检查滚动编号是否为12个字符。 second function checks if the name is not null. 第二个函数检查名称是否不为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. 页面上可能存在一些javascript错误,这些错误不允许第二个函数执行。 Please use browser's inspect element to explore it. 请使用浏览器的inspect元素进行浏览。

document.forms["myForm"]["id"].value you are are checking value of the textbox. document.forms [“ myForm”] [“ id”]。value您正在检查文本框的值。 Please document.forms["myForm"]["id"].value.length to validate it. 请document.forms [“ myForm”] [“ id”]。value.length进行验证。

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..";
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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