最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add two "on submit=" values to a form? - Stack Overflow

programmeradmin11浏览0评论

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?

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?

Share Improve this question edited Jan 27, 2014 at 21:34 BartoszKP 35.9k15 gold badges107 silver badges134 bronze badges asked Jun 24, 2010 at 3:35 JamesJames 211 silver badge2 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

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 bine 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)

  1. 暂无评论