-2

I'm using

$('#myform')[0].reset();

to clear HTML form fields when a clear button is clicked. I'm also using jquery.validate.js. So when the above runs, it triggers form validation. All form fields with any validation then display their error messages. How do I prevent this?

I have tried this but it didn't do anything:

$('#myform').removeAttr("nonvalidate");
Sparky
  • 98,165
  • 25
  • 199
  • 285
4thSpace
  • 43,672
  • 97
  • 296
  • 475
  • 1
    How are you telling the form to be validated? – David Jan 08 '15 at 00:16
  • As mentioned, I'm using jquery.validate and following that pattern. So I do $('#myform').validate({...my rules here...}); – 4thSpace Jan 08 '15 at 00:17
  • 3
    I don't know what `.reset()` is referring to for you but according to the docs it's `.resetForm()` - http://jqueryvalidation.org/Validator.resetForm – Deryck Jan 08 '15 at 00:20
  • Check here for reset(): http://stackoverflow.com/questions/27826381/clearing-form-input-fields-in-bootstrap – 4thSpace Jan 08 '15 at 00:22
  • You spelled `novalidate` wrong above. However, the `novalidate` attribute is dynamically placed there by jQuery Validate to stop HTML5 validation while you're using this plugin, so it has nothing to do with anything here. – Sparky Jan 08 '15 at 17:04
  • If your "reset" button is triggering the validation, then it sounds like you've incorrectly set your reset button as a `type="submit"`. Make it a `type="reset"`. – Sparky Jan 08 '15 at 17:07

1 Answers1

1

From the question you linked, the answer is what you want... All you have to do is capture the reset event and call v.resetForm().

var v = $('form').validate(); //etc etc whatever you have here, the important part is saving "v"

$('form').on('reset',function () {
    v.resetForm();
});

See it working here: http://jsfiddle.net/uuu8jerr/

Ryley
  • 21,046
  • 2
  • 67
  • 81
  • If your "clear" button isn't a reset button, you'd have to change the selector from `form` to your button id, and event to `click` and add a `$('#myform').get(0).reset()` to the handler. – Ryley Jan 08 '15 at 16:32
  • It might be worth mentioning that removing the `novalidate` attribute has nothing to do with this... plus the OP misspelled it. – Sparky Jan 08 '15 at 17:02