Exclusive Conditional Validation

Here’s an example , demonstrating only one active ValidationGroup out of many – during Submit.

<form id="form1" runat="server">
 <asp:ScriptManager ID="ScriptManager1" runat="server" />
 <script language="javascript" type="text/javascript">
  function ValidateNow() {
     var CurrValGrp = "";
     if ($get('<%= ddlCriteria.ClientID %>').value == 1) {
         CurrValGrp = "Group1";
     }
     else {
        CurrValGrp = "Group2";
     }
     My_PageClient_Validate(CurrValGrp);
     ValidatorUpdateIsValid();
     ValidationSummaryOnSubmit();
     if (!Page_IsValid) {
         return false;
     }
     return true;
  }

  function My_PageClient_Validate(CurrValGrp) {
     for (i = 0; i < Page_Validators.length; i++) {
        var val = Page_Validators[i];
        if (val.validationGroup == CurrValGrp) {
           val.style.display = "inline";
           ValidatorValidate(Page_Validators[i], CurrValGrp, null);
      }
      else {
        val.isvalid = true;
        val.style.display = "none";
    }
  }
  }
  </script>
 <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
 <div>
    <asp:DropDownList ID="ddlCriteria" runat="server" >
       <asp:ListItem Selected="True" Value="1">Criteria 1</asp:ListItem>
        <asp:ListItem Value="2">Criteria 2</asp:ListItem>
    </asp:DropDownList>
   <br /><br />
   <asp:TextBox ID="txt1" runat="server" ></asp:TextBox>
   <asp:RequiredFieldValidator ID="Req1" runat="server" ErrorMessage="Required 1" ControlToValidate="txt1" 
   ForeColor="Red"  Text="Required 1" ValidationGroup="Group1"></asp:RequiredFieldValidator> 
   <br />
   <asp:TextBox ID="txt2" runat="server" ></asp:TextBox>
  <asp:RequiredFieldValidator ID="Req2" runat="server" ErrorMessage="Required 2" ControlToValidate="txt2" 
  ForeColor="Red"  Text="Required 2" ValidationGroup="Group2"></asp:RequiredFieldValidator> 
  <br /><br />
  <asp:Button ID="btnsubmit" runat="server" Text="Submit" OnClientClick="return ValidateNow();"  
  onclick="btnsubmit_Click"/>
    </div>
    </form>

Advertisements
This entry was posted in General ASP.Net C#. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s