How to validate against Multiple validation groups?

Jeremy picture Jeremy · Mar 24, 2010 · Viewed 12k times · Source

I have two validation groups: parent and child

I have an add button that needs to only validate the child validation group which is easily done. The save button needs to validate against the parent and child validation groups, both client side and server side. I think I know how to do it server side by calling the Page.Validate("groupname") method for each group, but how can it be done client side?

Answer

CAbbott picture CAbbott · Mar 24, 2010

You should be able to accomplish this by creating a javascript function that uses Page_ClientValidate and then having the button call that function

<asp:Button ID="btnSave" Text="Save" OnClientClick="return validate()" runat="server" />

<script type="text/javascript">
    function validate() {
        var t1 = Page_ClientValidate("parent");
        var t2 = Page_ClientValidate("child");

        if (!t1 || !t2) return false;

        return true;
    }
</script>