Javascript – way to tell if all validators have been satisfied

asp.netcjavascriptnetvisual studio

OnclientClick of a button I hide a panel client side. I only want to hide it however if all input is valid. Is this possible in Javascript?

Best Answer

You can use the Page_IsValid property, it allows you to check whether the entire form is valid. You must call the Page_ClientValidate function before checking the value of Page_IsValid. This function is responsible for setting the value of the Page_IsValid property.

<asp:Panel ID="pnl" runat="server">
<asp:Button ID="btn" runat="server" Text="Click" OnClientClick="return Validate();" CausesValidation="false" />

<script type="text/javascript">
    function Validate()
    {
        if (typeof(Page_ClientValidate) == 'function')
            Page_ClientValidate();

        var panel = document.getElementById('pnl');         
        if(Page_IsValid)        
            panel.style.display = 'none';       
        else        
            panel.style.display = 'block';

       return false;        
   }
</script>