1

I'm using jQuery validation, I want to disable keyup validation for a certain element so I'm using the code below : -

$("form").validate({
    onkeyup: function (element) {
        if (element.id == 'tx_username') {
            return false;
        }
    }

This just disables it for everything and not just the desired element. Does anyone know how I can disable the keyup validation for just the element specified. 'tx_username' in this case

Shazoo
  • 685
  • 12
  • 25

3 Answers3

2

This just disables it for everything and not just the desired element.

When you use the onkeyup option, your custom function is going to completely over-ride the plugin's default function. Since you have no else statement, then nothing else will happen when the field is not a match for tx_username... effectively disabling onkeyup validation entirely.

onkeyup: function (element) {
    if (element.id == 'tx_username') {
        return false;
    }
}

The solution is to incorporate the default onkeyup code into yours...

onkeyup: function (element, event) {
    var excludedKeys = [
        16, 17, 18, 20, 35, 36, 37,
        38, 39, 40, 45, 144, 225
    ];
    if (element.id == 'tx_username') { // disable 'onkeyup' for this field
        return false;  
    } else { // otherwise, use the default code
        if (event.which === 9 && this.elementValue(element) === "" || $.inArray(event.keyCode, excludedKeys) !== -1) {
            return;
        } else if (element.name in this.submitted || element.name in this.invalid) {
            this.element(element);
        }
    }
}

DEMO: http://jsfiddle.net/ff77xbvb/


NOTE: This later version of the onkeyup function will ignore the following keys...

// Shift       => 16
// Ctrl        => 17
// Alt         => 18
// Caps lock   => 20
// End         => 35
// Home        => 36
// Left arrow  => 37
// Up arrow    => 38
// Right arrow => 39
// Down arrow  => 40
// Insert      => 45
// Num lock    => 144
// AltGr key   => 225
Sparky
  • 98,165
  • 25
  • 199
  • 285
0

Try Something like below.

     onkeyup: function (element, event) {

     if (event.which === 9 && this.elementValue(element) === "") {
           return;
     } else {
     if (element.id != 'tx_username') {
           this.element(element);
     }

Referred link - jQuery.validate.js onkeyup = true error

Community
  • 1
  • 1
koolhuman
  • 1,581
  • 15
  • 25
  • FYI @Shazoo - The code from that [old thread](http://stackoverflow.com/questions/15103289/jquery-validate-js-onkeyup-true-error) came from version 1.11.1 of the plugin. It's now up to version 1.15. See [my answer on this page](http://stackoverflow.com/a/36413930/594235) for the updated version. Notice how the `shift`, `ctrl`, `alt`, etc. keys are ignored by the latest `onkeyup` function. – Sparky Apr 06 '16 at 19:27
-1

Try using jQuery's .off() method:

$("#tx_username", form).off("keyup");

DEMO

mhodges
  • 10,938
  • 2
  • 28
  • 46