Please help! Validation plugin: addMethod with error message from server doesn't work well

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:

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3.     <head runat="server">
  4.         <script src="https://code.jquery.com/jquery-2.1.1.js"></script>
  5.         <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/jquery.validate.js"></script>
  6.     </head>
  7. <body>
  8.     <form id="_form">
  9.         <input id="_name" name="_name" data-rule-nameavailable="true" value="exists" />
  10.         <input type="submit" value="Sumbit" />
  11.         <script>

  12.         jQuery.validator.addMethod("nameavailable",
  13.             function (value, element) {
  14.                 return $.validator.methods.remote.call(this, value, element, {
  15.                     type: "POST",
  16.                     url: "ValidationService.svc/IsNameAvailable",
  17.                     contentType: "application/json; charset=utf-8",
  18.                     dataType: "json",
  19.                     data: JSON.stringify(
  20.                     {
  21.                         name: value
  22.                     }),
  23.                     dataFilter: function (data) {
  24.                         // WCF service return "d" object for security reason. Get rid of it.
  25.                         var x = (JSON.parse(data)).d;
  26.                         return JSON.stringify(x == '' ? true : x);
  27.                     }
  28.                 });
  29.             });

  30.         $("#_form").validate({
  31.             onkeyup: false,
  32.             onfocusout: false
  33.         });

  34.         </script>
  35.     </form>
  36. </body>
  37. </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!