title: Using the remote function of jquery validation
body: <p>I'm trying to figure out how I can turn this:</p>
  
  <pre><code>$('#username').blur(function(){
      $.post('register/isUsernameAvailable', 
             {"username":$('#username').val()}, 
             function(data){
                 if(data.username == "found"){
                     alert('username already in use');
                 }
             }, 'json');
  });
  </code></pre>
  
  <p>into something close to this:</p>
  
  <pre><code>rules: {
          username: {
              minlength: 6,
              maxlength: 12,
              remote: {
                  url: 'register/isUsernameAvailable',
                  type: 'post',
                  data: {
                      'username': $('#username').val()
                  } 
  
              }
          }
  </code></pre>
  
  <p>However I'm having a hard time finishing it off. What I want is instead of the alert to have it display the error message but I can set the message inside the actual jquery validation messages.</p>
  
  <p><a href="http://docs.jquery.com/Plugins/Validation/Methods/remote#options" rel="noreferrer">http://docs.jquery.com/Plugins/Validation/Methods/remote#options</a></p>
  
  <p><strong>UPDATE:</strong> </p>
  
  <p>For some reason its not doing it as a POST its doing it as a GET request and not sure why. Here's the updated code:</p>
  
  <pre><code>rules: {
          username: {
              minlength: 6,
              maxlength: 12,
              remote: {
                  url: 'register/isUsernameAvailable',
                  dataType: 'post',
                  data: {
                      'username': $('#username').val()
                  },
                  success: function(data) {
                      if (data.username == 'found')
                      {
                          message: {
                              username: 'The username is already in use!'
                          }
                      }
                  }
  
              }
          },
  </code></pre>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>Now I'm getting somewhere I'm back to getting the POST request. I'm getting two more problems. One of which is the fact that for another POST request to be done the user has to refresh the form. And the last problem is that if the returned username is found it DOES NOT show the error message.</p>
  
  <pre><code>rules: {
          username: {
              minlength: 6,
              maxlength: 12,
              remote: {
                  type: 'post',
                  url: 'register/isUsernameAvailable',
                  data: {
                      'username': $('#username').val()
                  },
                  dataType: 'json',
                  success: function(data) {
                      if (data.username == 'found')
                      {
                          message: {
                              username: 'The username is already in use!'
                          }
                      }
                  }
  
              }
          },
  </code></pre>
  
  <p><strong>UPDATE:</strong> </p>
  
  <pre><code>public function isUsernameAvailable()
  {
      if ($this-&gt;usersmodel-&gt;isUsernameAvailable($this-&gt;input-&gt;post('username')))
      {
          return false;
      }
      else
      {
          return true;
      }        
  }
  </code></pre>
  
  <p><strong>UPDATE 4:</strong></p>
  
  <p>Controller:</p>
  
  <pre><code>public function isUsernameAvailable()
  {
      if ($this-&gt;usersmodel-&gt;isUsernameAvailable($this-&gt;input-&gt;post('username')))
      {
          return false;
      }
      else
      {
          return true;
      }        
  }
  
  public function isEmailAvailable()
  {
      if ($this-&gt;usersmodel-&gt;isEmailAvailable($this-&gt;input-&gt;post('emailAddress')))
      {
          return false;
      }
      else
      {
          return true;
      }        
  }
  </code></pre>
  
  <p>MODEL:</p>
  
  <pre><code>/**
   * Check if username available for registering
   *
   * @param   string
   * @return  bool
   */
  function isUsernameAvailable($username)
  {
      $this-&gt;db-&gt;select('username');
      $this-&gt;db-&gt;where('LOWER(username)=', strtolower($username));
      $query = $this-&gt;db-&gt;get($this-&gt;usersTable);
      if ($query-&gt;num_rows() == 0)
      {
          return true;
      }
      else
      {
          return false;
      }
  }
  
  /**
   * Check if email available for registering
   *
   * @param   string
   * @return  bool
   */
  function isEmailAvailable($email)
  {
      $this-&gt;db-&gt;select('email');
      $this-&gt;db-&gt;where('LOWER(email)=', strtolower($email));
      $query = $this-&gt;db-&gt;get($this-&gt;usersTable);
      if($query-&gt;num_rows() == 0)
      {
          return true;
      }
      else
      {
          return false;
      }
  }
  </code></pre>