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

javascript - validation on submit button - Stack Overflow

programmeradmin0浏览0评论

I have a submit button like this:

<input class="create_button" name="commit" 
       onclick="return validate_activity();" 
       type="submit" value="Save">

I found that this button will always send request to the server whatever the validate_activity() return true or false?

What's the problem here?

UPDATE

Actually, I made a mistake in my validate_activity(), it makes me think it returned false, but it not.

I have a submit button like this:

<input class="create_button" name="commit" 
       onclick="return validate_activity();" 
       type="submit" value="Save">

I found that this button will always send request to the server whatever the validate_activity() return true or false?

What's the problem here?

UPDATE

Actually, I made a mistake in my validate_activity(), it makes me think it returned false, but it not.

Share Improve this question edited Jan 2, 2012 at 13:44 Rocky asked Dec 26, 2011 at 18:07 RockyRocky 5,7168 gold badges35 silver badges38 bronze badges 2
  • 1 Can you post what you have on validate_activity()? – Emmanuel N Commented Dec 26, 2011 at 18:13
  • Right, I made a mistake in my validate_activity(). – Rocky Commented Jan 2, 2012 at 13:43
Add a comment  | 

6 Answers 6

Reset to default 10

try doing same without return, e.g. onclick="validate_activity();" and check if your function returns false in case of invalidity

Try this...

<form action="...." onsubmit="submitForm()" name="form">

<input type="submit" value="Save">

</form> 


function submitForm(){

    if(validationfails){

        return false;
    }
    else  {
        document.form.submit();
        return true;
    }
 }
<input type="button" name="commit"
value="Save"
onclick="validate_activity(myform)" />

Inside the Javascript, when validated, do form.submit() or else return false.

I hope this helps ! This will validate your form and submit it. This submit will fail if the Javascript is disabled in your browser.

if you want the submit to proceed even if the javascript is disabled in the client browser ( means no validation but will submit), then

<input type="button" name="commit"
value="Save"
onclick="validate_activity(myform);return false;" />

I hope no validation shouldn't hit you at all. But then javascript is just client side and I hope you will have serious sanity checks on the server side anyways and of many, I think people use it for very basic field checks.

You should use the submit event of the form for validation..

<form action="..." method="..." onsubmit="return validate_activity()">
    <...>
    <input class="create_button" name="commit" type="submit" value="Save" />
    <...>
</form>

If you're doing form validation then validating it on button's onclick event, the way you're are trying makes no sense better you should use it on <form></form> onsumbmit event like this.

<form action="someURL" method="post" onsubmit="return validate_activity();">

</form>

I guess the problem is onclick="return validate_activity();", this return statement gets executed when clicking the submit button (not return false). The correct way is onclick="validate_activity();" and return false in validata_activity function, which will stop the default submit action.

发布评论

评论列表(0)

  1. 暂无评论