How to add two “on submit=” values to a form?

南楼画角 提交于 2019-12-06 07:36:32

Remove the returns from the onsubmit and add them to the functions:

onsubmit="return (formCheck(this) && validate_dropdown())"

onsubmit="return (formCheck(this) && validate_dropdown())"
user706872

I don't have enough reputation to vote up or down, but I can assure you that when having two returns, like: return formCheck(this) && return validate_dropdown(); it needs to be written like return formCheck(this) && validate_dropdown();

...leaving out the second 'return'.

So the replies with the most votes on this thread are actually wrong, and Anax's reply with 0 votes (at the time of writing) solved my problem. The others produced script errors in IE.

Your code will not work, because, as soon as the first function (formCheck) is executed, it will return a value, which will either allow or deny form submission.

If you have two or more functions you want to use at the same time, you can either combine their results or write a new function, which in turn will run both your validation functions and return the results.

Method A.

<form onsubmit="return (function1(this) && function2(this))">

Method B.

<script type="text/javascript">
function formValidation(myForm)
{
    var result =  function1(myForm);
    result = result && function2(myForm);
    return result;
}
</script>

<form onsubmit="return formValidation(this)">
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!