In Focus

Enable/Disable RequiredFieldValidator with Javascript

Use ValidatorEnable function from the Asp.net javacsript Script Library to Enable/Disable the validators on client side.

Sometimes you may need to enable/disable validators on client side. you can easily do this using ValidatorEnable function in the Asp.net javacsript Script Library. Here is an example:

  1. <script language="javascript" type="text/javascript">  
  2.     function EnableDisableVote() {  
  3.         var radio = document.getElementById('<%= rdoYesNo.ClientID %>');  
  4.         var radioArray = radio.getElementsByTagName("input");  
  5.         var isChecked = false;  
  6.         for (var i = 0; i < radioArray.length; i++) {  
  7.             var item = radioArray[i];  
  8.             if (item.type == "radio" && item.checked) {  
  9.                 isChecked = true;  
  10.                 if (i > 0) {  
  11.                     document.getElementById('<%= TextBoxCaseNotes.ClientID %>').disabled = false;  
  12.                     ValidatorEnable(document.getElementById('<%= RequiredFieldValidatorCasseNotes.ClientID %>'), true);   
  13.                 }  
  14.                 else {  
  15.                     document.getElementById('<%= TextBoxCaseNotes.ClientID %>').value = '';  
  16.                     document.getElementById('<%= TextBoxCaseNotes.ClientID %>').disabled = true;  
  17.                     ValidatorEnable(document.getElementById('<%= RequiredFieldValidatorCasseNotes.ClientID %>'), false);  
  18.                 }  
  19.                 break;  
  20.             }  
  21.         }  
  22.         if (isChecked && document.getElementById('<%= ChkIagree.ClientID %>').checked) {  
  23.             document.getElementById('<%= ButtonVote.ClientID %>').disabled = false;  
  24.         }  
  25.         else  
  26.             document.getElementById('<%= ButtonVote.ClientID %>').disabled = true;  
  27.     }  
  28. </script>