jQuery validate rule ONLY AT SUBMIT

后端 未结 6 2176
感动是毒
感动是毒 2020-12-09 02:14

Say i have a form that looks up city, state locations. if the user enters an incorrect city, state, i want the form to be able to check the database against the users input

6条回答
  •  -上瘾入骨i
    2020-12-09 03:16

    As an alternative an a more straightforward way you can pass a function to the onfocusout and onkeyup settings of the jquery.validator to decide if an element has to be validated in those events or not, as this :

    jQuery.validator.defaults.onfocusout = function (element, event) {
        // detectect if is the element you dont want validate
        // the element has to have the attribute 'data-control="mycitycontrol"'
        // you can also identify your element as you please
        if ($(element).data("control") === "mycitycontrol") return;
        if (!this.checkable(element) && (element.name in this.submitted || !this.optional(element))) {
            this.element(element);
        }
    }
    jQuery.validator.defaults.onkeyup = function (element, event) {
        // detectect if is the element you dont want validate
        // the element has to have the attribute 'data-control="mycitycontrol"'
        // you can also identify your element as you please
        if ($(element).data("control") === "mycitycontrol") return;
        if (event.which === 9 && this.elementValue(element) === "") {
            return;
        } 
        else if (element.name in this.submitted || element === this.lastElement) {
            this.element(element);
        }
    }
    

提交回复
热议问题