Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do it asynchronously.</p> <p>Create a global variable, I call it ajax_done_and_successful_flag, that you initialize to false at the beginning of the program. You will set this to true or false at various places in your Ajax functions such as your Ajax success function or your Ajax error function. </p> <p>Then you need to add a submit handler at the bottom of your validate function. </p> <pre><code>submitHandler: function(form) { if (ajax_done_and_successful_flag === true) { form.submit() } } </code></pre> <p>The problem is the code is not executing in a linear way.<br> Put a bunch of Firebug's console.log statements in your code.<br> Observe the sequence of execution. You will see that your<br> Ajax response will come back last, or whenever it feels like it.<br> That's why you need the submitHandler AND the global flag<br> to force the validate function to wait for correct Ajax<br> results before the form is submitted. </p> <p>Any output to the screen from the Ajax response, needs to be done in the Ajax functions, such as the success function and the error function.<br> You need to write to the same location as the validate function's success/error functions.<br> This way the Ajax error messages blend in with the validate function's error function.<br> This concept may seem a bit tricky.<br> The idea to keep in mind is that the success and error functions in the validate function are writing to the same location as the success and error functions in the Ajax call, and that is okay, that is how it should be.<br> The location of my error messages is right next to where the user types the input. This creates a nice user experience that I think you are asking for. </p> <p>Here is my code sample. I simplified it. </p> <p>I am running jQuery-1.7.1<br> and jQuery validation plug-in 1.6<br> I am using Firefox 14.0.1 and I also tried it on Chrome 21.0 successfully. </p> <pre><code> var ajax_done_and_successful_flag = false; // Add methods ... $.validator.addMethod("USERNAME_NOT_DUPLICATE", function (value, element) { return this.optional(element) || validate_username_not_duplicate( ); }, "Duplicate user name."); // validate $(document).ready(function ( ) { $('#register_entry_form form').validate({ rules: { username: { required: true, minlength: 2, maxlength: 20, USERNAME_PATTERN: true, USERNAME_NOT_DUPLICATE: true }, ... errorPlacement: function (error, element) { element.closest("div").find(".reg_entry_error").html(error); }, success: function(label) { label.text('Success!'); } , submitHandler: function(form) { if (ajax_done_and_successful_flag === true ) { form.submit(); } } }); }); /* validation functions*/ function validate_username_not_duplicate() { var username_value = $('#username').val(); // whatever is typed in $.ajax({ url: "check_duplicate_username.php", type: "POST", data: { username: username_value }, dataType: "text", cache: false, //async: false, timeout: 5000, error: function (jqXHR, errorType, errorMessage) { ajax_done_and_successful_flag = false; if ( errorType === "timeout" ) { $('#username').closest("div").find(".reg_entry_error").html("Server timeout, try again later" ); } else if ... }, success: function (retString, textStatus,jqXRH) { if ( retString === "yes") { // duplicate name // output message next to input field $('#username').closest("div").find(".reg_entry_error").html("Name already taken."); ajax_done_and_successful_flag = false; } else if ( retString === "no") { // name is okay // output message next to input field $('#username').closest("div").find(".reg_entry_error").html("success!"); ajax_done_and_successful_flag = true; } else { console.log("in validate_username_duplicate func, success function, string returned was not yes or no." ); $('#username').closest("div").find(".reg_entry_error").html("There are server problems. Try again later."); ajax_done_and_successful_flag = false; } } // end success function }); // end ajax call return true; // automatically return true, the true/false is handled in // the server side program and then the submit handler } </code></pre> <p>The reg_entry_error is the place right beside the text input. Here is a simplified code sample of the form. </p> <pre><code> &lt;label class="reg_entry_label" for="username"&gt;Username&lt;/label&gt; &lt;input class="reg_entry_input" type="text" name="username" id="username" value="" /&gt; &lt;span class="reg_entry_error" id="usernameError" &gt;&lt;/span&gt; </code></pre> <p>I hope this answers your question.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload