Validation isn't being done when I need it to be
I have a page that has been using the jQuery validation plugin successfully to validate several text fields. There are several submit buttons on the page, and I have configured the validator as follows:
- _validator = $("#ftsEditTripForm").validate({
- onkeyup: false,
- submitHandler: function() {}
- });
I register the following click event handler for all the submit buttons on the page:
- function clickHandler(jqEvent) {
- var action = jqEvent.target.id;
- if (action !== "cancel") {
- _form$.validate();
- if (_validator.numberOfInvalids() === 0) {
- submitForm(action);
- }
- } else {
- confirmCancel(action);
- }
- }
- function submitForm(action) {
- $(".flowEvent").attr("name", "_eventId_" + action);
- if (action === "submit" ) {
- $("input[name=status]").val("S");
- } else {
- $("input[name=status]").val("D");
- }
- _form$[0].submit();
- }
This has been working fine. Validation isn't done until the user clicks one of the submit buttons and, if that button is the Cancel button, the user is asked to confirm they want to lose any changes they've made on the page before the page is actually submitted. So we're stopping page submission if the user clicks the Cancel button and then clicks the No button, saying they don't want to continue with the cancel operation.
Now I've added tabs to a navigation bar, and I need to do the same kind of thing if the user clicks on a tab other than the tab for the page she's on now. I've defined the following click event handler for the tabs:
- function tabClickHandler(jqEvent) {
- var target = jqEvent.target;
- while ( ! $(target).hasClass("navTab")) {
- target = target.parentNode;
- }
- var tabNumber = target.dataset.tabnumber;
- _form$.validate();
- if (_validator.numberOfInvalids() === 0) {
- $("input[name=tabNumber]").val(tabNumber);
- submitForm("navbar");
- }
}
My problem is that _validator.numberOfInvalids() always returns 0 when I click on a tab. I've put a breakpoint in my validation code, and it isn't called when I click on a tab. As a result, the page is always submitted, even if there are validation errors.
I've tried a number of things -- various values for the validation plugin's onsubmit and submitHandler options, changing the tab <div>s to <button>s -- without success.
I hope someone can point me in the right direction. I have to get this working one way or another, and I'd like to keep using the validation plugin.
Thanks for reading this far.
Thom