3

The build in remote attribute in asp.net mvc 3 does the validation "onchange".
I want it to validate on blur, is there a way to custom it? or there is something else for doing so? I'm sure it's a very common need.

bkaid
  • 51,465
  • 22
  • 112
  • 128
gdoron
  • 147,333
  • 58
  • 291
  • 367
  • 1
    possible duplicate of [ASP.NET Remote Validation only on blur?](http://stackoverflow.com/questions/5407652/asp-net-remote-validation-only-on-blur) – Judah Gabriel Himango Nov 17 '11 at 23:29
  • Does this answer your question? [ASP.NET Remote Validation only on blur?](https://stackoverflow.com/questions/5407652/asp-net-remote-validation-only-on-blur) – kevinpo Jul 05 '22 at 21:56

2 Answers2

7

You could set default values like this and disable validation when a key is pressed:

$.validator.setDefaults({ onkeyup: false });
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks for the answer, can you please explain me what that line of code does? I didn't see any explantion in the line you wrote. Thanks again! – gdoron Aug 21 '11 at 18:03
  • 1
    @gdoron, as the documentation explains, the `setDefaults` method allows you to override default configuration values for the jquery validate plugin. Setting `oneyup` to false indicates that validation should not be performed when the user releases a key. – Darin Dimitrov Aug 21 '11 at 20:28
  • Is there any different between the code you wrote and the code below written by offbysome? – gdoron Aug 21 '11 at 21:23
  • 1
    @gdoron, yes there is. When you use the `setDefaults` method you are explicitly setting default values for all validation plugins on the current page. With the code below you are attaching the `validate` plugin additionally and thus conflicting with the ASP.NET MVC 3 unobtrusive validator plugin. – Darin Dimitrov Aug 23 '11 at 15:29
  • In a different page I need to disable the onkeyup for only one property. How can I do that – gdoron Nov 20 '11 at 07:57
3

Assuming you are using jQuery validation and not MS Ajax Validation in ASP.NET MVC 3, you would turn off onchange validation (technically its onKeyUp validation) by using this:

$(".selector").validate({
   onkeyup: false
});
bkaid
  • 51,465
  • 22
  • 112
  • 128