Using multiple active ValidationGroups at submit, on clientside.

Here’s an example showing more than 1 ValidationGroup, being active,during Form-Submit.This example takes care of both server as well as client aspects.

<form id="form1" runat="server">
 <script language="javascript" type="text/javascript">
  function ValidateNow() {
  My_PageClient_Validate("Group1");
  My_PageClient_Validate("Group2");
 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) {
           ValidatorValidate(Page_Validators[i], CurrValGrp, null);
        }
     }
 }
 </script>
 <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
 <div>
    <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>

Now code-behind

protected void btnsubmit_Click(object sender, EventArgs e)
    {
        Page.Validate("Group1");
        Page.Validate("Group2");
        if (!Page.IsValid)
        {
            return;
        }
    }

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