jQuery validation with submit handler

Posted by James on Stack Overflow See other posts from Stack Overflow or by James
Published on 2009-08-02T08:18:42Z Indexed on 2010/04/01 8:03 UTC
Read the original article Hit count: 274

Filed under:
|

I setup the form validation using jQuery validation plug-in's validate method and I have a submit handler that modifies the input element's value (I use YUI editor and it needs saveHTML() call to copy the iframe's content to the textarea element.). When submitting the form, I want the validator to validate the form after executing my submit handler. But it doesn't execute my submit handler if it is registered after the validate call.

For example,

<form id="form1" action="/test">
    <input type="text" name="txt1" id="txt1" />
    <input type="submit" value="submit" />

$(document).ready(function() {
    $("#form1").submit(function() {
    	$("#txt1").val("123456");
    });

    $("#form1").validate({
    	rules: {
    		txt1: {
    			maxlength: 5
    		}
    	}
    });
});

The form is validated after my submit handler so submit is canceled.

$(document).ready(function() {
    $("#form1").validate({
    	rules: {
    		txt1: {
    			maxlength: 5
    		}
    	}
    });

    $("#form1").submit(function() {
    	$("#txt1").val("123456");
    });
});

However if I change the order the form is validated before my submit handler.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about jquery-validate