Search code examples
asp.net-mvcasp.net-mvc-3

ASP.NET Remote Validation only on blur?


I'm using the remote validation in MVC 3, but it seems to fire any time that I type something, if it's the second time that field's been active. The problem is that I have an autocomplete box, so they might click on a result to populate the field, which MVC views as "leaving" it.

Even apart from the autcomplete thing, I don't want it to attempt to validate when they're halfway through writing. Is there a way that I can say "only run validation n milliseconds after they are finished typing" or "only run validation on blur?"


Solution

  • MVC 3 relies on the jQuery Validation plugin for client side validation. You need to configure the plugin to not validate on key up.

    You can switch it globally off using

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

    See http://docs.jquery.com/Plugins/Validation/Validator/setDefaults and the onkeyup option here http://docs.jquery.com/Plugins/Validation/validate.