Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Looking at the markup of <a href="http://jsfiddle.net/F2Re4/" rel="nofollow">your jsFiddle</a>,</p> <pre><code>&lt;input type="button" onclick="validate()" value="Validate"&gt;&lt;/input&gt; &lt;input type="button" onclick="resetForm()" value="Reset Form"&gt;&lt;/input&gt; </code></pre> <p>1) <code>input</code> elements do not have a closing tag. They may or may not need to be "self-closing" depending on your <code>doctype</code>, but there is never any such thing as an <code>&lt;/input&gt;</code> tag.</p> <p>2) Inline JavaScript is ugly and unnecessary when you use jQuery. <code>onclick</code> handlers can easily be removed and replaced with jQuery <code>.on('click')</code>...</p> <pre><code>$('#reset').on('click', function () { var form = $("#myForm"); form.validate().resetForm(); }); </code></pre> <p>3) You do not need a <code>validate</code> function attached to your "validate" button. Simply change the button into a <code>type="submit"</code> and it will be validated automatically. Otherwise, you would need another <code>.on('click')</code> handler and a <code>.valid()</code> within to trigger a validation test.</p> <pre><code>&lt;input type="submit" id="validate" value="Validate" /&gt; &lt;input type="button" id="reset" value="Reset Form" /&gt; </code></pre> <hr> <p><strong>Quote OP:</strong></p> <blockquote> <p>... <code>validator.resetForm()</code> to clear the messages, but this <code>invalidHandler</code> is never called and the 'error' css class still there in the form.</p> </blockquote> <p>As per documenation, <code>invalidHandler</code> is only called when the form is invalid. If you reset the form, the form is no longer invalid. Therefore, the logic used within your <code>invalidHandler</code> is flawed.</p> <pre><code>var errors = validator.numberOfInvalids(); if (errors) { $(this).addClass("form-error"); } else { // this will never be called because invalidHandler // is never called when there are no form errors //$(this).removeClass("form-error"); } </code></pre> <p>There is nothing in this plugin that will automatically add/remove the class of the <code>&lt;form&gt;</code> element itself. The plugin only automatically adds/removes classes from the various data input elements. Therefore, if you manually add a class to the <code>&lt;form&gt;</code> element, then you're going to have to manually remove it when you reset the form.</p> <pre><code>$(document).ready(function () { $("#myForm").validate({ // your rules &amp; options }); $('#reset').on('click', function () { var form = $("#myForm"); form.validate().resetForm(); // reset validation on form form.removeClass("form-error"); // remove error class from form }); }); </code></pre> <p><strong>DEMO: <a href="http://jsfiddle.net/F2Re4/11/" rel="nofollow">http://jsfiddle.net/F2Re4/11/</a></strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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