简体   繁体   中英

Validate multiple controls Asp.Net

So, I have a login box and a signup box on one page. Both have RequiredField validator. Right now, fields for both sign up and login get validated when I click either login or signup.

However, I want it, if sign up is clicked, only signup fields get validated and if login is clicked on login fields get validated.

Could you please help me out here?

Many Thanks!

As Dooie said u have to assign VAlidation groups like this

       <table width="100%"> 
       <tr> 
          <td> 
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
                <asp:RequiredFieldValidator 
   ID="RequiredFieldValidator1" runat="server" 
   ErrorMessage="RequiredFieldValidator" 
   ValidationGroup="Group1" ControlToValidate="TextBox1"> 
    </asp:RequiredFieldValidator> 
           </td>            
           </tr> 
        <tr> 
            <td> 
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 
                <asp:RequiredFieldValidator 
     ID="RequiredFieldValidator2" runat="server" 
      ErrorMessage="RequiredFieldValidator" 
      ValidationGroup="Group2" ControlToValidate="TextBox2"> 
      </asp:RequiredFieldValidator> 
            </td> 
         </tr> 
        <tr> 
            <td> 
                   <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>  
     <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"                                                
    ValidationGroup="Group3" ControlToValidate="TextBox3"> 
   </asp:RequiredFieldValidator> 
           </td> 
        </tr> 
        <tr> 
         <td> 
              <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> 
               <asp:RequiredFieldValidator 
                ID="RequiredFieldValidator4" runat="server"       ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox4">  
                  </asp:RequiredFieldValidator>             
           </td> 
       </tr> 
      <tr> 
           <td> 
                <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="javascript:return   validatePage();" /> 
               <asp:Button ID="btnCancel" runat="server" Text="Cancel" /> 
           </td> 
       </tr> 

for all your validations,and then.... From the above markup you can make out that there are four required field validators and out of the four validators three have validation group property defined. In such a scenario when you click the submit button only the validator which doesn't have validation group will be executed ie RequiredFiedlValidator4 will only be executed. Since there are more than one validation group assigning button' ValidationGroup wont work as it will execute only validator controls which belong to the assigned validaiton group. Other validator controls belonging to other validation group won't execute. Also there is no way to specify multiple validaiton group using the ValidationGroup property of the button control.

The way to solve this problem is to call Page_ClientValidate javascript function. Page_ClientValidate is a javascript function generated by ASP.NET. The function takes validation group name as an argument. The javascript function which gets called when the submit button is clicked is pasted below.

         <script language="javascript" type="text/javascript"> 
      function validatePage() 
      { 
  //Executes all the validation controls associated with group1 validaiton Group1. 
    var flag = Page_ClientValidate('Group1'); 
   if (flag) 
   //Executes all the validation controls associated with group1 validaiton Group2. 
        flag = Page_ClientValidate('Group2'); 
    if (flag) 
   //Executes all the validation controls associated with group1 validaiton Group3. 
       flag = Page_ClientValidate('Group3'); 
  if (flag) 
   //Executes all the validation controls which are not associated with any validation group. 
      flag = Page_ClientValidate(); 
    return flag; 
     } 
 </script>

Hope this helps................

Assign ValidationGroup="SignUp" to the signup validators and button and ValidationGroup="Login" to the login validators and button

when you do this only the validation in the same group will fire

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM