Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would love to solve the problem using the power of unbind function( <a href="http://api.jquery.com/unbind/" rel="nofollow">http://api.jquery.com/unbind/</a> ).The good part of this solution is that we don't have to attach the newly added fields to the validator object manually.The full code is given below:</p> <p>/* server-side.html file */ <code></p> <p></p> <pre><code>&lt;html&gt; &lt;!-- This is a jQuery Tools standalone demo. Feel free to copy/paste. http://flowplayer.org/tools/demos/ Do *not* reference CSS files and images from flowplayer.org when in production Enjoy! --&gt; &lt;head&gt; &lt;title&gt;jQuery Tools standalone demo&lt;/title&gt; &lt;!-- include the Tools --&gt; &lt;script src="http://cdn.jquerytools.org/1.2.6/full/jquery.tools.min.js"&gt;&lt;/script&gt; &lt;!-- standalone page styling (can be removed) --&gt; &lt;link rel="stylesheet" type="text/css" href="http://static.flowplayer.org/tools/css/standalone.css"/&gt; &lt;link rel="stylesheet" type="text/css" href="http://static.flowplayer.org/tools/demos/validator/css/form.css"/&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="myform"&gt; &lt;fieldset&gt; &lt;h3&gt;Sample registration form&lt;/h3&gt; &lt;p&gt; Enter bad values and then press the submit button. &lt;/p&gt; &lt;p&gt; &lt;label&gt;website *&lt;/label&gt; &lt;input type="url" name="url" required="required" /&gt; &lt;/p&gt; &lt;p&gt; &lt;label&gt;name *&lt;/label&gt; &lt;input type="text" name="name" pattern="[a-zA-Z ]{5,}" maxlength="30" /&gt; &lt;/p&gt; &lt;p&gt; &lt;label&gt;age&lt;/label&gt; &lt;input type="number" name="age" size="4" min="5" max="50" /&gt; &lt;/p&gt; &lt;p id="terms"&gt; &lt;label&gt;I accept the terms&lt;/label&gt; &lt;input type="checkbox" required="required" /&gt; &lt;/p&gt; &lt;button type="submit"&gt;Submit form&lt;/button&gt; &lt;button type="reset"&gt;Reset&lt;/button&gt; &lt;/fieldset&gt; &lt;/form&gt; &lt;script&gt; // initialize validator and add a custom form submission logic var submitEvent = function(){$("#myform").validator().submit(function(e) { var form = $(this); // client-side validation OK. if (!e.isDefaultPrevented()) { // submit with AJAX $.getJSON("server-fail.js?" + form.serialize(), function(json) { // everything is ok. (server returned true) if (json['valid'] === 'true') { if($("#testField").length == 0){ $("#terms").before('&lt;p&gt; &lt;label id="testField"&gt;email *&lt;/label&gt;&lt;input type="email" name="email" required="required" /&gt;&lt;/p&gt;'); } else { alert("new field validated successfully"); } form.unbind("submit"); submitEvent(); // server-side validation failed. use invalidate() to show errors } else { form.data("validator").invalidate(json); } }); // prevent default form submission logic e.preventDefault(); } });}; submitEvent(); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p></code></p> <p>/* server-fail.js file */</p> <p><code> </p> <pre><code>{ "name": "Invalid name. Our apologies", "age": "You are too young", "valid": "true" } </code></pre> <p></code></p> <p>You can try this code to make the functionality more dynamic.Enjoy :)</p>
 

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