Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is is possible to to have a depends on a jQuery remote validation?

I am using jQuery remote validation to check if the description is already being used.

 Description: {
                    required: true,
                    maxlength: 20,
                    remote: function () {
                        var newDescription = $("#txtDescription").val();
                        var dataInput = { geoFenceDescription: newDescription };
                        var r = {
                            type: "POST",
                            url: "/ATOMWebService.svc/DoesGeoFenceDescriptionExist",
                            data: JSON.stringify(dataInput),
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            dataFilter: function (data) {
                                var x = (JSON.parse(data)).d;
                                return JSON.stringify(!x);
                            }
                        };
                        return r;
                    }
                },

The problem I have is that this remote validation occurs when the user has NOT modified the text box and comes back saying the description has been used because it found it self in the database.

So is it possible to only run the remote validation if the text field is different to what was originally in it?

I noticed the the jQuery required validation has a depends option, but I couldn't get it to work with the remote call.

like image 223
David Kethel Avatar asked Feb 14 '12 05:02

David Kethel


People also ask

What is the jQuery “remote” validation rule?

I only just found out that the jQuery validation plugins has a validation rule called “remote” which can be used to make an ajax request instead of specifying a custom rule which has an ajax call it in. This will save you heaps of time with those custom validation rules.

How do I disable the onkeyup validation plugin?

By default the validation plugin will send off an ajax request for a remote rule every key you press causing too many ajax requests being sent to validate fields. One way to disable this is to deactivate the onkeyup validation so that the remote rule is only validated via ajax once you have finished typing into the input.

How to pass data through custom validation rules in jQuery?

This will save you heaps of time with those custom validation rules. Basic jQuery Form Validation Tutorial (2mins). Example: Checking if a users email is already registered. As you can see to pass through data you can simply use the key pair syntax so the request sent below the data is “&[email protected]”.


1 Answers

Here's a solution for using remote with depends: https://github.com/jzaefferer/jquery-validation/issues/431

like image 149
Ben Avatar answered Oct 08 '22 18:10

Ben