Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

How to disable validation group using javascript

0.00/5 (No votes)
22 Nov 2011CPOL 22.1K  
Enable/Disable validation group using javascript
JavaScript
script type="text/javascript">        
function HasPageValidators()        
{            
  var hasValidators = false;                      
  try            
  {                
    if (Page_Validators.length > 0)                
      {                    
        hasValidators = true;                
      }            
  }            
  catch (error) { }                      
  return hasValidators;        
}              

function ValidationGroupEnable(validationGroupName, isEnable)        
{            
  if (HasPageValidators())            
  {                
    for(i=0; i < Page_Validators.length; i++)                
    {                    
      if (Page_Validators[i].validationGroup == validationGroupName)               
      {
        ValidatorEnable(Page_Validators[i], isEnable);                    
      }                
    }            
  }        
}  


ASP.NET
<form id="form1"  runat="server">        
  <div>            
  <input type="button" value="Disable required field validators!"  önclick="ValidationGroupEnable('vgRequiredFields', false)" />            <input type="button" value="Enable required field validators!"  önclick="ValidationGroupEnable('vgRequiredFields', true)" />          
  <table>              
  <tr>                  
   <td style="width: 229px">                      
    <asp:Label ID="lblName" runat="server" Text="Name: " />
    <asp:TextBox ID="txtName" runat="server" />                      
    <asp:RequiredFieldValidator ID="rfvName" runat="server" ErrorMessage="*"  ControlToValidate="txtName" ValidationGroup="vgRequiredFields" />
   </td>                                  
  </tr>              
  <tr>                   
   <td style="width: 229px">                       
    <asp:Label ID="lblEmail" runat="server" Text="Email: " />
    <asp:TextBox ID="txtEmail" runat="server"/> 
    <asp:RequiredFieldValidator ID="rfvEmail" runat="server" ErrorMessage="*" ControlToValidate="txtEmail" ValidationGroup="vgRequiredFields" /> 
    <asp:RegularExpressionValidator ID="revEmail" runat="server"ErrorMessage="!" ControlToValidate="txtEmail" ValidationGroup="vgRegularExpressions"ValidationExpression="^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$" />
   </td>              
  </tr>          
  </table>       
 </div> 
</form>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)