Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I preventDefault(); inside of an ajax callback?

I'm doing some form validation, and I'm having trouble with what I'm trying to accomplish. I want to be able to validate my zip code on blur of the field, but also call the same function to validate zip on submit of the form, and prevent the form from being submitted if the zip code is invalid. My code (generically) goes something like this.

function validateZipCode(event){
    $.getJson(
        url,
        params,
        function(data){
            if(data.response === false){
                someError.show();
                event.preventDefault(); //stop the form from being submitted
            }    
        }
    );
}

$('#someForm').submit(function(event){
    validateZipCode(event);
});

$('#zipInput').blur(function(event){
    validateZipCode(event);
})

I've tried using the methods located in this jQuery pass more parameters into callback post, but I still can't seem to accomplish what I need. Any help is appreciated.

like image 880
Nick Avatar asked Jun 01 '11 14:06

Nick


People also ask

What is preventDefault in Ajax?

preventDefault() method stops the default action of an element from happening. For example: Prevent a submit button from submitting a form. Prevent a link from following the URL.

Why include event preventDefault (); in a form script?

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 will happen if the name of the Ajax callback function?

When the ajax succeeds and invokes myCallback , it will run synchronously. So myCallback is like other functions, you call it first it will run first or you call it last it will run last. THEY WILL NOT RUN AT THE SAME TIME.

What does event preventDefault () do?

Event.preventDefault() The preventDefault() method of the Event interface tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be.


2 Answers

No, by the time the callback from the AJAX event fires, the event will have already bubbled and the form submitted. One way you could accomplish this is cancel the form from submitting and then submit it manually if it passes the requirements from the AJAX request.

like image 151
Robert Avatar answered Oct 12 '22 17:10

Robert


You need to prevent the event's default action before it's bubbled up and handled by the browser. $.getJson returns control and allows the validateZipCode function to finish first, so you need to do this:

function validateZipCode(event){
    event.preventDefault(); //stop the form from being submitted
    $.getJson(
        url,
        params,
        function(data){
            if(data.response === false){
                someError.show();
            }
        }
    );
}

$('#someForm').submit(function(event){
    validateZipCode(event);
});

$('#zipInput').blur(function(event){
    validateZipCode(event);
})
like image 35
Milimetric Avatar answered Oct 12 '22 16:10

Milimetric