jQuery.validate.js onkeyup = true error

前端 未结 5 1188
南笙
南笙 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条回答
  • 2020-12-05 08:22

    How about extending onkeyup:

        $('#signup-form').validate({
    
            onkeyup: function(element) {
                var element_id = $(element).attr('id');
                if (this.settings.rules[element_id].onkeyup !== false) {
                    $(element).valid();
                }
            },
    

    So now within rules you can do this:

            rules: {
                username: {
                    required: true,
                    minlength: 3,
                    maxlength: 25,
                    onkeyup: false, /* on blur validation */
                },
                password: {
                    required: true,                   
                    passwordmeter: true,
                    onkeyup: true, /* on keyup validation */
                },
    

    By extending onkeyup we made it a default behaviour, so sticking onkeyup: true is optional.

    0 讨论(0)
  • 2020-12-05 08:26

    @Sparky is right, The correct answer is to remove the onkeyup option, my fiddle was working because I removed the onkeyup option not because I changed it to keyup.

    By default the validation will happen on keyup event, if you want to turn off this feature then you have to add the setting onkeyup: false.

    See the updated demo.

    Demo: Fiddle

    0 讨论(0)
  • 2020-12-05 08:27

    boolean values are not valid values for onkeyup event. Instead add a function, on what is needed to be done. Try something like:

        onkeyup: function(element) {
               $(element).valid(); 
        }
        onblur: function(element) { 
             $(element).valid(); 
        }
    
    0 讨论(0)
  • 2020-12-05 08:36

    You actually need to set the onkeyup option to a function that accepts the element being validated as a parameter, so try changing:

    onkeyup: true, 
    

    to

    onkeyup: function(element) {$(element).valid()}
    
    0 讨论(0)
  • 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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题