Tuesday, September 24, 2013

Dynamically Enable or Disable Required Field Validator


To enable or disable the required field validator control based on any selection
Enable or Disable ASP.Net Validation on client side

//Syntax:
ValidatorEnable(ValidatorContronName,Boolean);

//Explanation:
ValidatorContronName - This is ClientID of the Validation control.
Boolean - true(Enable) / false(Disable)

//Example:
rfvOther: is a Required Field Validator
ValidatorEnable(document.getElementById('<%=rfvOther.ClientID%>'), false);


Explonation#2:
'ValidatorEnable' or 'ValidatorUpdateDisplay' will immediately validate the associated control and show any validation messages. If this is not wanted because you just want to toggle the enabled/disabled switch but wait until form submission to validate, then the 2nd method of calling enabled on the object is the preferred method. If you do want immediate validation, then this can be done in a single line of code passing in the ID of the RequiredFieldValidator as displayed below:
ValidatorEnable($get('<%=RequiredFieldValidator1.ClientID %>'), true);
However, if you want only to enable/disable the validator, use the code below and do not make any additional calls to the built in JS functions. The error from previous posts states to set the .enable property yet there is no such thing. You must set the .enabled property on the server control. The code below shows this:
var validator = $get('<%=RequiredFieldValidator1.ClientID %>');
validator.enabled = true;

No comments:

Post a Comment