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.
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.
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.
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]”.
Here's a solution for using remote
with depends
: https://github.com/jzaefferer/jquery-validation/issues/431
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With