Ajax validation with jquery?

I am using jquery validation plugin.

In my form, I need to check if the nickname is used or not.

To do this, they provide a remote key to call ajax. For me, the ajax call works correctly and returns true or false. But its resolution, even if the return value is false, should not happen.

My verification code looks like

 $(function() { $("#myform").validate({ rules: { fullName: { required: true, minlength: 5 }, nickName: { required: true, minlength: 4, alphaNumeric: true, remote: { url: "NickNameChecker", type: "post", data: { nickName: function() { return $("#nickName").val(); }}, success: function(data) { return data; } } } }, messages: { fullName: { required: "Please Enter Your Full Name.", minlength: "Full Name should have minimum 5 letters." }, nickName: { required: true, minlength: "Nick Name should contain minimum 4 letters.", remote: "This nickname is already in use." } } }); }); 

Any suggestions would be appreciated !!!

Thanks!

Decision:

The solution is only in my code.

I just deleted some of the success and tried. It works great!

+7
source share
2 answers

(Extended explanation from the comment above):

The success parameter is not valid for validating remote jQuery validation. You should simply provide a validation access URL that returns true or false value / error message:

The server resource is called through $ .ajax (XMLHttpRequest) and receives a pair of keys / values, the corresponding name of the element being checked and its value as a GET parameter. The response is evaluated as JSON and must be true for valid elements and can be either false, undefined or null for invalid elements using the default message; or string

For more information on how to use this option, see the documentation for the remote . Your code will probably look something like this:

 $(function() { $("#myform").validate({ rules: { fullName: { required: true, minlength: 5 }, nickName: { required: true, minlength: 4, alphaNumeric: true, remote: { url: "NickNameChecker", type: "post", data: { nickName: function() { return $("#nickName").val(); } } } }, messages: { fullName: { required: "Please Enter Your Full Name.", minlength: "Full Name should have minimum 5 letters." }, nickName: { required: true, minlength: "Nick Name should contain minimum 4 letters.", remote: "This nickname is already in use." } } }); }); 
+1
source

The jQuery Validation team reported another way to handle non-boolean APIs in this GitHub issue .

The proposed method uses the jQuery.ajax () dataFilter property.

 form.validate({ rules: { username: { required: true, remote: { url: "users2.json.php", dataFilter: function(data) { var json = JSON.parse(data); if(json.status === "success") { return '"true"'; } return "\"" + json.error + "\""; } } } } }); 
0
source

All Articles