25

Is there a way to disable the jQuery Validation for a certain validator (creditcard) so that it only occurs onblur, instead of onkeyup?

Based on the jQuery Validator documentation I thought I could do something like this:

$(function () {
    $("[data-val-creditcard]").validate({
        onkeyup: false
    })
});

However, it doesn't seem to be working.

I also tried doing the following on my validator:

public class CreditCardValidator : DataAnnotationsModelValidator<CreditCardAttribute>
{
    string _message;

    public CreditCardValidator(ModelMetadata metadata, ControllerContext context, CreditCardAttribute attribute)
        : base(metadata, context, attribute)
    {
        _message = attribute.ErrorMessage;
    }

    public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
    {
        var rule = new ModelClientValidationRule
        {
            ErrorMessage = _message,
            ValidationType = "creditcard"
        };
        rule.ValidationParameters.Add("onkeyup", false);
        return new[] { rule };
    }
}

It doesn't work either, but I was just taking a stab at the appropriate use of ValidationParameters.

It is kind of annoying to be entering a credit card number in a form and having it randomly change from invalid to valid, then back to invalid.

Any ideas? Thanks!

Sam
  • 9,933
  • 12
  • 68
  • 104

6 Answers6

21

$.validator.setDefaults doesn't work for unobtrusive validation as the validator is initialized internally.

To change the settings on forms that use unobtrusive validation you can do the following:

var validator = $("form").data("validator");
if (validator) {
    validator.settings.onkeyup = false; // disable validation on keyup
}
Ben Foster
  • 34,340
  • 40
  • 176
  • 285
  • http://aspnet.uservoice.com/forums/41201-asp-net-mvc/suggestions/3583148-update-jquery-validate-unobtrusive-implementation- – Blake Niemyjski Jan 23 '13 at 23:58
  • This would disable the keyup validation trigger for all elements, no? He was asking how to disable it for only 1 element. – TweeZz Jan 30 '14 at 08:57
21

Ok guys,

I was in the same problem and found this thread: http://old.nabble.com/-validate--onkeyup-for-single-field-td21729097s27240.html

The idea is basically to overwrite the keyup event and return false. So in your specific case you'll need to add:

$('#my-form').validate({
   rules: {
     [...]
   }
} 
// Disable keyup validation for credit card field
$("[data-val-creditcard]").keyup(function() { return false } );

And you'll see that your credit card field is only checked on blur or submit (but the rest is working on keyup also).

I was looking for the same solution, and found that the answer here could be improved, so I thought it would be nice to share it here.

mTorres
  • 3,590
  • 2
  • 25
  • 36
  • 2
    Fantastic tip on disabling keyup by returning false. – Sam Sep 19 '12 at 04:35
  • This was the only approach I found that was able to suppress the keyup validation. It was excessive as I was making an ajax call in order to validate the input. In the end, I set a data attribute on the input element which was controlled by the ajax result of an onchange event for the input element. This allowed jquery validate to have a simple return true or false while still making a request to the server for database validation. – Travis J Sep 16 '14 at 23:27
  • 1
    There were several events to suppress, I made a simple placeholder for them, `var fnfalse = function(){ return false; }`, and then used that to block the functionality, `$(element).keyup(fnfalse).blur(fnfalse).focusout(fnfalse);`. – Travis J Sep 16 '14 at 23:28
  • 2018 and still working! Thanks a lot, you saved my day. – ClownCoder Jun 11 '18 at 00:25
  • The link is dead unfortunately :( – Crushermike Dec 19 '19 at 19:40
9

I realise this is an old post, but none of the responses appear to answer this question,

How to disable onkeyup and enable onblur?

I was looking for this so thought I would share the answer.

As Ben Foster rightly said, onkeyup can be disabled by doing the following

var validator = $("form").data("validator");
if (validator) {
    validator.settings.onkeyup = false;
}

To enable validation onblur we can use onfocusout

validator.settings.onfocusout = function(element)
{
    $(element).valid();
};

So our code to disable onkeyup and enable onblur look like this

var validator = $("form").data("validator");
        if (validator)
        {
            validator.settings.onkeyup = false; 
            validator.settings.onfocusout = function(element)
            {
                $(element).valid();
            };
        }
Andrew
  • 5,215
  • 1
  • 23
  • 42
Colin Bacon
  • 15,436
  • 7
  • 52
  • 72
5

Don't know how to set it to a specific field, but you could try this to disable keyup validation (for all fields):

$.validator.setDefaults({
   onkeyup: false
})

See

Community
  • 1
  • 1
epzee
  • 568
  • 8
  • 22
  • Yeah, I saw that. I like it on most of the fields, just not the credit card fields or the remote validation fields (I'd rather wait to hit the website once onblur instead of on every change of a character). May just be a limitation I have to live with. I'll probably do the global setDefaults like you mentioned above... – Sam Nov 05 '11 at 20:59
  • ...At first I was setting this up using to make sure the document and scripts had loaded and were ready to use, but it didn't work. I had to pull it out to get it to work, e.g.: . However, I'm wondering what will happen if the user hasn't finished downloading the scripts yet. I've put them at the bottom of the document instead of in the head. – Sam Nov 05 '11 at 21:03
  • Ended up just disabling the "onkeyup" – Sam Dec 01 '11 at 07:41
1

You could also use something like the above,

$("form").validate({
    onfocusout: false,
    onkeyup: false
});

In case you have custom rules then,

$("form").validate({
    onfocusout: false,
    onkeyup: false,
    rules: {
        "name1": {
            required: true,
            email: true,
            maxlength: 100
        },
        "name2": {
            required: true,
            number: true
        }
    }
});

This answer in the jQuery forum helped me a lot.

Georgios Syngouroglou
  • 18,813
  • 9
  • 90
  • 92
0

You could set the defaults to off in the onfocus event of the credit field, then turn them in the onblur. Seems hacky, but could work.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291