0

Got an screen with several forms, one of them has an input (submit). The JS binding click event for that input is:

$('input#next').click(function(e){
   e.preventDefault();
   e.stopPropagation();
   ... some other checks ...
   bSubmit = dateField.val().match(/\d{2}\/\d{2}\/\d{4}/);
   return bSubmit;
});

The issue is that the form is send anyway, which should not happens.

3
  • Try changing to $("#your_form_id").submit(function(e) since the user also can submit form with enter. Commented Feb 11, 2014 at 23:23
  • Do you mean to do the validation in the submit event for the form, instead in the click event of the submit input, don't you? Commented Feb 11, 2014 at 23:25
  • I would do so, yes. Think about it, if the user clicks enter while filling the form, your current code will not prevent the form from sending 'naturally'. Commented Feb 11, 2014 at 23:25

3 Answers 3

3

If returned bSubmitp value is other than false, then the form will be submitted.

With a return true you are saying that the button must work add is intended... and
-the submit button is for sending the form-

If you want to disable at all cost, I suggest a predefined return false.

Could be:

  • on the tag in like:

    <form onsubmit="return false">
    
  • in your function like you have now but creating a conditional value, like:

    <form onsubmit="return yourFunction()">
    
  • with like:

    $('form').submit(function() {
        // do stuff
        return false;
    });
    
  • others..

Edit

Additionally.. you could change the input type:submit to just a button
Then send the form with jQuery (after validation).

And to prevent form submit when enter key is pressed as @Daniel Lisik point it out...
need to acomplish two condition (without javascript)

  1. No input type:submit buttons present in the form
  2. More than one input field.
Sign up to request clarification or add additional context in comments.

Comments

0

As Daniel said, you should put your code into the jQuery submit function

This method is a shortcut for .on( "submit", handler ) in the first variation, and .trigger( "submit" ) in the third.

The submit event is sent to an element when the user is attempting to submit a form. It can only be attached to elements. Forms can be submitted either by clicking an explicit , , or , or by pressing Enter when certain form elements have focus.

https://api.jquery.com/submit/

Example code straight from the jQuery website

$( "#target" ).submit(function( event ) {
  alert( "Handler for .submit() called." );
  event.preventDefault();
});

Comments

0

Changing the button type will disable the submit of the input already from start up:

$(document).ready(function() {

    $('input#next').attr("type", "button");

    // own code
    $('input#next').click( function() {
        // ...
    });
});

And then you can add your own code below.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.