The server is called synchronously, so the click handler blocks the display of the browser.
As far as I know, there is no cross-browser way to force update and update ui from javascript. But you may be lucky with the introduction of a special solution for this. A Google search should help with this.
Another way to handle this would be to prevent the default processing of the click event and submit the form after the asynchronous validation was successful.
IMHO's best solution is to check each field asynchronously while the user fills out the form and only activates the save button when all checks are completed. This can be done with .blur() so that each entry is checked when the user moves on to the next. User input validation can be implemented by a combination of .keypress() and setTimeout() . Here is a brief example to illustrate this:
<!doctype html> <html lang="en"> <head></head> <body> <form id="form" action="form.html"> <label for="item1">Validate email on blur:</label><input id="item1" type="text"/><span id="item1-status">Invalid</span><br/> <lavel for="item2">Validate email while writing</lavel><input id="item2" type="text"/><span id="item2-status">Invalid</span><br/> <button id="Save" type="submit">Save</button> </form> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <script type="text/javascript"> $(function () { var validState = { "set": function (name, state) { this[name] = state; updateUI(); }, "item1": null, "item2": null } function formIsValid() { var res = true; for(var name in validState) { res = res && validState[name]; } return res; } function validString(val) { return val==null ? "" : val ? "Valid" : "Invalid" } function updateUI() { $("#item1-status").text( validString(validState.item1) ); $("#item2-status").text( validString(validState.item2) ); $("#Save").prop("disabled", !formIsValid()); } function isEmail(str) { return /\b[A-Z0-9._%+-] +@ [A-Z0-9.-]+\.[AZ]{2,4}\b/i.test(str); } function validateItem1() { validState.set("item1", isEmail($("#item1").val())); } function validateItem2() { validState.set("item2", isEmail($("#item2").val())); } </script> </body> </html>
Tas
source share