jQuery.validate.js onkeyup = true error

前端 未结 5 1200
南笙
南笙 2020-12-05 08:05

With my jquery validation configuration, I get the following error when setting onkeyup to true. It works if I set it to false, but I don\'t get validation feedback until I

5条回答
  •  -上瘾入骨i
    2020-12-05 08:43

    onkeyup is enabled by default so you do not need to set it to true. If you do, you break the functionality already built into the plugin.

    You have three options:


    1) Leave the onkeyup option out of .validate(). This keeps onkeyup functionality enabled by default. (edit: "by default" means that validation occurs on every "key-up" event only after the field is initially validated by another event.)

    DEMO: http://jsfiddle.net/ZvvTa/


    2) onkeyup can be set to false to disable this option.

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


    3) Replace onkeyup with your own callback function to modify how it operates. (Demo uses default function)

    DEMO: http://jsfiddle.net/ZvvTa/2/

    Below is the default, unmodified, onkeyup callback function:

    onkeyup: function( element, event ) {
        if ( event.which === 9 && this.elementValue(element) === "" ) {
            return;
        } else if ( element.name in this.submitted || element === this.lastElement ) {
            this.element(element);
        }
    }
    

    See: http://docs.jquery.com/Plugins/Validation/validate#toptions


    EDIT:

    By default, the plugin does not do any "key-up" validation until after the field is initially validated by another event. ("Lazy" validation)

    So here is a more properly modified version of the onkeyup callback function that will provide immediate onkeyup validation. ("Eager" validation)

    DEMO: http://jsfiddle.net/QfKk7/

    onkeyup: function (element, event) {
        if (event.which === 9 && this.elementValue(element) === "") {
            return;
        } else {
            this.element(element);
        }
    }
    

提交回复
热议问题