title: jquery - return value from callback function (in post request) into the function its inside of?
body: <p>I have a javascript function that posts data to a validation script and grabs a value from there. The callback function on the post request returns a boolean value, and I'm trying to get the <em>entire</em> function to return that boolean value. Right now, the callback function returns the correct value, but the function itself doesn't return anything. Here's the code:</p>
  
  <pre><code>function validate(request_type, request_text) {
      $.post("http://www.example.com/ajax/validate.php",{
          type: request_type, 
          text: request_text
      }, function(data) {
          return (data == "valid");
      });
  }
  </code></pre>
  
  <p>I realise that this is sort of a "synchronous" call, and that's not what AJAX is about, but I already have numerous functions in validate.php (database calls, etc.) that I can't implement in Javascript, and I saw threads like <a href="https://stackoverflow.com/questions/3537434/cant-get-correct-return-value-from-an-jquery-ajax-call">this one</a> that talk about using some form of handler.</p>
  
  <p>How would I write a simple handler that will make either the variable <code>data</code> or the result of the boolean comparison <code>data == "valid"</code> available when I use it in an <code>if/else</code> statement (which is where this function is supposed to be used)?</p>
  
  <p>EDIT: For example, one of the <code>if</code> statements that will be using the boolean result:</p>
  
  <pre><code>if (!validate('password',pass_new)) {
          $('#pass_new').addClass('error');
          $('#pass_confirm_new').addClass('error');
          $(error_string.format('Please enter a valid password.')).insertAfter('#pass_confirm_new');
          $('#pass_text_short').hide();
          $('#pass_text_long').show();
  </code></pre>
  
  <p>EDIT: The function called with the <code>onsubmit</code> event in my HTML form:</p>
  
  <pre><code>function valid_pass_sett() {
      //code to remove errors left over from previous submissions - snipped
      pass_old = $('input[name=pass_old]').val();
      pass_new = $('input[name=pass_new]').val();
      pass_confirm_new = $('input[name=pass_confirm_new]').val();
      //some if statements that don't involve AJAX requests - snipped
      if (!validate('password',pass_new)) {
          $('#pass_new').addClass('error');
          $('#pass_confirm_new').addClass('error');
          $(error_string.format('Please enter a valid password.')).insertAfter('#pass_confirm_new');
          $('#pass_text_short').hide();
          $('#pass_text_long').show();
          return false;
      }
  
      return true;
  }
  </code></pre>
  
  <p>I haven't edited this code to include the updated code that's been posted, but my question is how I <code>return false</code> from it to stop form submission?</p>