javascript - Hitting enter in any textbox in chrome triggers the form submit, even when there is no submit button - Stack Overfl

I am building a site which uses jQUery validation plugin and want things validated before submitting th

I am building a site which uses jQUery validation plugin and want things validated before submitting the form. My code looks like follows

<form>
  <input type="button" value="Submit the Form" onclick="validateAndSubmit()" />
</form>
<script language="javascript">

   function validateAndSubmit(){
   //do some validation and then submit 
 }

</script>

In Firefox, this works perfectly. In Chrome, when I hit enter anywhere in the page, the form submit is triggered and validation doesn't work either. Is there something to avoid this ? Shouldn't the browser not submit a form when we hit an enter if there is no submit button?

I am building a site which uses jQUery validation plugin and want things validated before submitting the form. My code looks like follows

<form>
  <input type="button" value="Submit the Form" onclick="validateAndSubmit()" />
</form>
<script language="javascript">

   function validateAndSubmit(){
   //do some validation and then submit 
 }

</script>

In Firefox, this works perfectly. In Chrome, when I hit enter anywhere in the page, the form submit is triggered and validation doesn't work either. Is there something to avoid this ? Shouldn't the browser not submit a form when we hit an enter if there is no submit button?

Share Improve this question asked Jun 30, 2010 at 9:10 Ritesh M NayakRitesh M Nayak 8,06314 gold badges51 silver badges78 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

use this syntax:

<form onsubmit="return validateAndSubmit()">
...

if you need to catch the return-key maybe you can handle it by binding an keydown event to the input and perform some action on keyCode #13

Try this method:

<form onsubmit="return validateAndSubmit(this);">
  <input type="submit" value="Submit the Form"/>
</form>
<script language="javascript">

   function validateAndSubmit(form_obj){
     if(some_variable == 'correct_value') {
        form_obj.submit();
        return true;
     } else {
        alert('Wrong value');
        return false;
     }
   //do some validation and then submit 
 }

</script>

I'm not sure if there's a standard regarding this or not.

Regardless, you can save yourself the trouble altogether by simply adopting a stronger strategy: implement the validation as an onsubmit action on the form, rather than an onclick action for the button. I almost never use buttons in forms; having to do so for yours would only throw me off, and that's not good for users.

So anyway. Form onsubmit is the way to go. And I'd appreciate it if you used unobtrusive Javascript instead of the HTML attributes :)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745375880a4624996.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信