jquery - Form Validation with ajax request -
i have general validation method validatefields application. script check every field needs validated against several conditions , increment counter.
if(validate_for_nil(field) == true){ // return true , continue other validations } else { //stop further actions , raise error } if(validate_max_chars(field) == true){ //continue other validations }else{ //stop , raise error }
similarly uniqueness validation, need send ajax request. problem is, function not waiting ajax response.it check condition undefined ajax response.how can make entire function wait till ajax response comes? async:false in ajax request not convenient me. please here code:
function form_validation() { $("[data-required=true]:visible").each(function(){ element = $(this); errors = validate_fields(element); total_errors = total_errors + errors; }) if(total_errors == 0) { //submit form }else{ //do nothing } } function validate_fields(field) { error = 0 if(validate_for_nil(field) == true){ // return true , continue other validations } else { error = error+1 raise_error() } if(validate_max_chars(field) == true){ //continue other validations }else{ //counter increment , raise error } if(validate_uniqueness(field) == true) { // method calls ajax request // return true }else { // counter increment , raise error } }
use ajax success/error function(s):
$.ajax({ cache: false, url: "some url", data: { }, success: function (data) { //do stuff here }, error: function (ajaxcontext) { alert(ajaxcontext.responsetext) } });
Comments
Post a Comment