Please help! Validation plugin: addMethod with error message from server doesn't work well
Hi,
I want to create a custom validation method which will call server and server will return an error message. I use WCF web service but I guess the issue will happen with any kind of web service.
Here is the code:
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <script src="https://code.jquery.com/jquery-2.1.1.js"></script>
- <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/jquery.validate.js"></script>
- </head>
- <body>
- <form id="_form">
- <input id="_name" name="_name" data-rule-nameavailable="true" value="exists" />
- <input type="submit" value="Sumbit" />
- <script>
-
- jQuery.validator.addMethod("nameavailable",
- function (value, element) {
- return $.validator.methods.remote.call(this, value, element, {
- type: "POST",
- url: "ValidationService.svc/IsNameAvailable",
- contentType: "application/json; charset=utf-8",
- dataType: "json",
- data: JSON.stringify(
- {
- name: value
- }),
- dataFilter: function (data) {
- // WCF service return "d" object for security reason. Get rid of it.
- var x = (JSON.parse(data)).d;
- return JSON.stringify(x == '' ? true : x);
- }
- });
- });
-
- $("#_form").validate({
- onkeyup: false,
- onfocusout: false
- });
-
- </script>
- </form>
- </body>
- </html>
Note: I disabled validation on "onkeyup" and "onfocusout" events, only "onsubmit" should trigger it.
First time I click "Submit" button it works well and shows error message from server. But if I click the button immediately second time, it shows: "
Warning: No message defined for _name
". Seems like a bug in the plugin. Please suggest if there is a workaround.
Thanks!