March 14, 2012

jQuery Submit will not submit form

Question by Prince

I have a for that I am submitting with this button:

<button id="registerButton" type="submit" name="submit" style="margin-left: 300px;">Click to Complete</button>

I am doing error checking with this jQuery:

$('#registerButton').bind('click',function(){
    if($('#formElem').data('errors')){
            alert('Please correct the errors in the Form');
            return false;
        }
});

When I go to submit the form it will pop up with the alert if there are errors detected on the form. However, if there are no errors the button doesn’t do anything. What am I doing wrong?

Answer by Starx

Bind the validation on the submit event of the form.

$("#formid").submit(function(e) { 
    if($(this).data('errors')){
        //display error
        e.preventDefault(); //stop the submit
    } else {
       //do something or nothing
       //but it will submit the form
    }

});

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!