Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery Validation Plugin - compare two form fields in one remote call
    text
    copied!<p>I'm trying to use jQuery's validate plugin for a changing name of the user. I managed to use the remote option to check if the username is found in my database. Below is my working example. However, if Peter from the example form doesn't change his name, the field is invalid because the name is already in database. I somehow need to include the condition - if #new_name is the same as #current_name, the #new_name field is valid. Is it possible?</p> <p>jQuery Validation Plugin</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function(){ $("form").validate({ rules: { new_name: { required: true, remote: { url: "names.php", type: "post", } } }, messages: { new_name: { remote: jQuery.validator.format("{0} is already taken, please enter a different name.") } } }); }); &lt;/script&gt; </code></pre> <p>HTML form:</p> <pre><code>&lt;form method="post" id="myForm" action=""&gt; &lt;!-- User enters a new name --&gt; &lt;input id="new_name" name="new_name" value="peter" type="text" /&gt; &lt;!-- This is a current username, populated from the database --&gt; &lt;input id="current_name" name="current_name" value="peter" readonly="readonly" /&gt; &lt;input type="submit" name="submit" id="submit"/&gt; &lt;/form&gt; </code></pre> <p>Remote call: names.php</p> <pre><code>&lt;?php $request = trim(strtolower($_REQUEST['new_name'])); // Here is the array of all user names in my database. $names = array('peter','jane','bill','george'); $valid = 'true'; foreach($names as $name) { if( strtolower($name) == $request ) { $valid = 'false'; } } echo $valid; ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload