20

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 blur the field, and I'm looking to get validation on each keyup:

$("#signupForm").validate({
        onkeyup: true,        
        onclick: false
        // rules omitted for brevity
    }

I get the following error:

TypeError: validator.settings[eventType].call is not a function
validator.settings[eventType].call(validator, this[0], event);
jquery.validate.js line 391
Sparky
  • 98,165
  • 25
  • 199
  • 285
Adam Levitt
  • 10,316
  • 26
  • 84
  • 145

5 Answers5

41

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()}
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
  • 3
    Simply removing the `onkeyup` option restores the "onkeyup" functionality already built into the plugin, which is somewhat more complex than `$(element).valid()`. – Sparky Feb 27 '13 at 04:28
  • 1
    @AdamLevitt, removing `onkeyup` entirely restores its default functionality, which is "on key-up". See: http://jsfiddle.net/ZvvTa/ – Sparky Feb 27 '13 at 16:58
  • Sparky, what's the difference between onkeyup and keyup? The default functionality for some reason isn't working for me. If I remove onkeyup completely, it doesn't work as desired. But with the solution in this answer, it does work. I'd prefer your solution if I could get it to work. – Adam Levitt Feb 27 '13 at 17:16
  • 1
    @AdamLevitt, `onkeyup` is an valid option for this plugin. `keyup` is nothing... it's a made-up word as far as this plugin is concerned. For the proper way to get **immediate** "on key-up" validation, see the edits on my answer. – Sparky Feb 27 '13 at 19:24
37

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);
    }
}
Sparky
  • 98,165
  • 25
  • 199
  • 285
  • Sparky, thanks so much for your dedicated efforts. Would recommend that I just manually put this function into the instance of the onkeyup? Or is it safe to replace the default implementation in the jquery.validate.js code? I'm assuming the former. – Adam Levitt Feb 27 '13 at 19:27
  • @AdamLevitt, YES, your initial assumption is correct. No need to modify the Validate plugin. It's meant to be implemented within `.validate()` initialization, just like in [my jsFiddle](http://jsfiddle.net/QfKk7/). – Sparky Feb 27 '13 at 19:31
  • Thanks @Sparky ... your third solution solved my "onfocusout" problem that doesn't work the first time just like u said. `onfocusout: function (element, event) { if (event.which === 9 && this.elementValue(element) === "") { return; } else { this.element(element); } }` – MarioAraya Dec 24 '15 at 16:30
5

@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

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
3

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(); 
    }
Anusha
  • 419
  • 1
  • 4
  • 7
  • 1
    True. However, if the OP wants validation to occur on every keyup event, he simply needs to remove the `onkeyup` option in order to restore the "onkeyup" functionality already built into the plugin. It's also not as simple as `$(element).valid()`. – Sparky Feb 27 '13 at 04:30
  • 2
    Additionally, you are only **half** correct by stating _"boolean values are not valid values for onkeyup event"_. As per documentation, a boolean `false` is the valid way to disable this option. Only a boolean `true` is not valid. – Sparky Feb 27 '13 at 19:38
  • Finally, `onblur` is not a valid option for this plugin. It's called `onfocusout`. – Sparky Sep 30 '15 at 00:48
  • Thank you. after some hours, search and ... see your answer and worked. – Milad Ghiravani Mar 07 '17 at 14:11
3

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.

Artur Kedzior
  • 3,994
  • 1
  • 36
  • 58
  • **Does not work and causes a bunch of JavaScript errors**: https://jsfiddle.net/L7vza07w - Also, plugin options are intended to be global for the entire form, they are never assigned on a field-by-field basis within the rules object. – Sparky Jun 21 '17 at 16:52
  • 1
    BTW - within the `.validate()` method, it should be `this.element(element)`, never `$(element).valid()`. In certain situations, the latter can cause an infinite loop – Sparky Jun 21 '17 at 16:53
  • docs for this.element(): https://jqueryvalidation.org/Validator.element/ – Fanky Jan 26 '21 at 09:38
  • Why can `onkeyup` be written in `rules` ? In Document, `onkeyup` is only allowed to be in options of `validate()` – Ho.Chun Sep 10 '22 at 01:55
  • see: https://forum.jquery.com/topic/jquery-validate-onkeyup-for-single-field – Ho.Chun Sep 10 '22 at 09:05