javascript - How to send Asynchronous AJAX call to serverside in JQuery validation plugin addmethod() -


i working on task deliver own framework using jquery validation plugin need validate crud forms client-side server-side.and it's important forms not static created dynamically "handlebar.js".

so mapped client side validations when input fields created class.

eg -

<form id="form-asset-create">   <input type="text" class="my own custom validation client side" /> </form> 

i have custom method client side validation call server side , check created asset added or not.but need send ajax call asynchronously when user inputs data. here below

and have javascript file called clientsidevalidation.js have validations on client side.

$.validator.addmethod("isuniquefield", function(value, element) {   var elementname = element.getattribute('name');   var data = '%22name%22 : %22' + value + '%22';   var result = false;   $.ajax({     type: "get",     url: caramel.url("/apis/assets?type=gadget&q=" + data),     datatype: "json",     async: true,     success: function(data) {       result = data.list.length <= 0;       $.validator.messages.isuniquefield = "the " + elementname + " taken";     },     error: function(xhr, thrownerror) {       console.log("error " + xhr.responsetext + "  " + thrownerror);     }   });   return result; }, $.validator.messages.isuniquefield); 

i have tried way cant proper error message while have property async : true , when change false working fine. can suggest me way approach task.is there way send ajax request asynchronously in custom validation method.

i have call validation of client side when form ready , when submit button clicked create asset.

$('#form-asset-create').validate();

suggestion appreciated , in advance

you have validate form on submit using if condition.

for example

$('#form-asset-create').on('submit', function(){   if($('#form-asset-create').valid()){       /* start ajax call or further process here*/   } }); 

you have add class required validation , message dynamically created textbox.

for i.e.

<input type="text" name="email" id="email" class="form-control required email isuniquefield" data-msg-required="this field required." data-msg-email="please enter valid email."   data-msg-isuniquefield="please enter unique value." placeholder="email  *" value=""> 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -