开发者

How to add two "on submit=" values to a form?

开发者 https://www.devze.com 2023-01-05 01:05 出处:网络
I am trying to validate my form using two separate JavaScript functions: <form onsubmit=\"return formCheck(this); return validate_dropdown();\"

I am trying to validate my form using two separate JavaScript functions:

<form onsubmit="return formCheck(this); return validate_dropdown();"  
      action="somepage.php" 
      method="post" 
      name="something">

When I add just 开发者_高级运维one on submit value, each function works fine individually, when I add two only the first function's validation works, not the second. What is wrong here?


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

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


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


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)">
0

精彩评论

暂无评论...
验证码 换一张
取 消