Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-enabling Submit after prevent default

I am trying to unbind or reenable the prevent default so my form will submit on good data.

I have tried multiple examples. Here is my code and some of the examples i tried.

This code works great for what i want to. Just the last thing and resetting the div which i can implement after i get this.

function lengthRestriction(elem, min, max) {
    var uInput = elem.value;
    if (uInput.length >= min && uInput.length <= max) {
        return true;
    } else {
        var cnt = document.getElementById('field');
        cnt.innerHTML = "Please enter between " + min + " and " + max + " characters";
        elem.focus();
        $('#ShoutTweet').submit(function(e) {
            e.preventDefault();

            //bind('#ShoutTweet').submit();
            //$('#ShoutTweet').trigger('submit'); 
        });
    }
}​

i have a jsbin set up too http://jsbin.com/ebedab/93

like image 225
Michael Hahn Avatar asked Mar 25 '12 00:03

Michael Hahn


People also ask

How do I submit a form after event preventDefault?

// Prevent form submit if any entrees are missing $('form'). submit(function(e){ e. preventDefault(); // Cycle through each Attendee Name $('[name="atendeename[]"]', this). each(function(index, el){ // If there is a value if ($(el).

Does preventDefault stop form submission?

The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur. For example, this can be useful when: Clicking on a "Submit" button, prevent it from submitting a form.

What is the opposite of preventDefault?

As you probably might have already worked out based on the simple explanation above: the opposite of event. preventDefault() is nothing. You just don't prevent the event, by default the browser will allow the event if you are not preventing it.


3 Answers

Don't try to set up and cancel a submit handler from within your validation function, do it the other way around: call the validation from within a single submit handler, and only call .preventDefault() if the validation fails:

$(document).ready(function() {

    $('#ShoutTweet').submit(function(e) {
       if (/* do validations here, and if any of them fail... */) {
          e.preventDefault();
       }
    });

});

If all of your validations pass just don't call e.preventDefault() and the submit event will then happen by default.

Alternatively you can return false from your submit handler to prevent the default:

    $('#ShoutTweet').submit(function(e) {
       if (!someValidation())
          return false;

       if (!secondValidation())
          return false;

       if (someTestVariable != "somevalue")
          return false;

       // etc.
    });
like image 118
nnnnnn Avatar answered Oct 05 '22 08:10

nnnnnn


I'm not completely sure what you are asking, but if your goal is to destroy your custom submit handler, then use this:

$("#ShoutTweet").unbind("submit");

This assumes that you have a normal (not Ajax) form.

like image 26
Chris Laplante Avatar answered Oct 05 '22 09:10

Chris Laplante


Just call submit on the form

$('#ShoutTweet').submit();
like image 33
max Avatar answered Oct 05 '22 10:10

max