Monday, April 22, 2013

ASP.Net MVC 3 jQuery Remote Validation - Some Gotchas

If you have a requirement where you need to validate few fields on the fly , like creating a unique username, you might find remote validation concept quite handy.

There is a good explanation (with example) here

There are several small issues you might run into when using Remote Validation concept in ASP.Net MVC3
1. Validator called twice
This generally happens if you have the script maintained at the page level and master (layout) page, then there is a chance that validation would be called twice. Remove the script reference from either of the places.
2. Validation happening on every keypress, but you would like to have it on blur (lost focus i.e.)
This can be handled by disabling the keyup for validator globally as shown below

$(document).ready(function () {
    $.validator.setDefaults({onkeyup: false})