0

I have a form with some required fields. If the user clicks a button, validation should fire, and their input will be processed. However, after processing, I want to reset/clear the form and allow them to enter more information. After I call form.reset() and validator.resetForm() though, the validation messages are shown because the form has been cleared. What is the proper way to clear the form and ensure that no validation messages appear? Here's the complete fiddle illustrating the issue. Here's the code snippet that I'd like to get working:

$('#btnGo').click(function(){
    // clear the form
    $('#frmFoo')[0].reset();
    // AFTER this call to resetForm, validation message is shown again.  Why?
    validator.resetForm();
});

Thanks,

Andy

1 Answer 1

1

The jQuery Validate plugin, by default, treats your button element as a submit, which is why the validation messages come right back during/after the reset.

You simply need to add type="button" to your button so that it doesn't trigger a submit.

<button type="button" id="btnGo">Do Something and Clear Form</button>

DEMO: http://jsfiddle.net/AxucF/1/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.