views:

193

answers:

1

Using MicrosoftMvcValidation.js for client side validation, is there an easy way of programatically (in javascript) turning the validation off and on for particular fields?

Is it possible for example to use jquery to bind/unbind the events?

+1  A: 

Imran Baloch's Blog has your answer:

http://weblogs.asp.net/imranbaloch/archive/2010/06/20/disabling-client-side-validation-for-disabled-input-controls-in-asp-net-mvc.aspx

It involves a customization to MicrosoftMvcValidation.debug.js

Change this:

if (!Sys.Mvc._validationUtil.stringIsNullOrEmpty(errorMessage)) {
    Array.add(errors, errorMessage);
}

To this:

var inptEl = $get(context.fieldContext.elements[0].id);
if (!Sys.Mvc._validationUtil.stringIsNullOrEmpty(errorMessage) && !inptEl.disabled) {
    Array.add(errors, errorMessage);
}
else
    Sys.UI.DomElement.removeCssClass(inptEl, Sys.Mvc.FieldContext._inputElementErrorCss);
AndrewDotHay

related questions