title: How to create a JQuery validator that makes ajax request
body: <p>I need to create a JQuery validator that will make an ajax request and check if user input is valid. My ajax request is the 'getIsCodeValid'.</p>
  <p><strong>This is obviously an async operation but the validator method returns the response before the async. I need to return the response after the async operation.</strong></p>
  <p>What I have so fare is the following:</p>
  <pre><code>var isValid = false;
  jQuery.validator.addMethod(&quot;isCodeValid&quot;, function(value, element) {
      var jqxhr = $.when(getIsCodeValid(value, element).done(function(data) {
          if(data==&quot;valid&quot;) {
              isValid = true;
          } else {
              isValid = false;
          }
      }));
      return isValid;
  }, &quot;* Code is invalid&quot;);
  </code></pre>
  <p>This will first return false and then set the 'isValid' to true.</p>
  <p>How do I make the validator to return the value of 'isValid' after it is set by the ajax request ?</p>