4

I have a simple form.

Input fields, checkboxes, radio buttons and finally SUBMIT button.

I use jQuery to perform AJAX validation, however when a user presses ENTER inside the input field it submits the form!

How do I stop this from happening on this form (not all input fields)?

hakre
  • 193,403
  • 52
  • 435
  • 836
st4ck0v3rfl0w
  • 6,665
  • 15
  • 45
  • 48

2 Answers2

6

suppose your inputs for which you don't want to submit on enter has class noSubmit then use this code:

$(function(){
     $("input.noSubmit").keypress(function(e){
         var k=e.keyCode || e.which;
         if(k==13){
             e.preventDefault();
         }
     });
 });
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
2

You can use the onsubmit handler to trap form submission and perform validation:

<form onsubmit="return validate();">
...
</form>

<script type="text/javascript">
function validate() {
  // return true to submit form or false to stop
}
</script>
casablanca
  • 69,683
  • 7
  • 133
  • 150