user1980 0 Newbie Poster

hello,

I am trying to enable validators using javascript and they get enabled as expected but when I am trying to disable them they give an error and then get disabled. I think I am missing a logic on how to disable them.

My code is supposed to validate two text boxes where if either one of them is filled the other has to be filled too or none of them have to be filled.(these text boxes appear only when a checkbox is checked)

can somebody please guide me. here is my code...excuse me for my huge code..(you can directly copy this directly to your system to test)..thank you for your time

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
 <meta http-equiv="Page-Enter" content="Alpha(opacity=100)" />

<script language="javascript" type="text/javascript">
    
      
      function Check() 
        { alert("checking");
         
            // GPA and highest GPA in the class
            var Valgpa = document.getElementById("<%=gpa.ClientID%>").value;  
            var Valclassgpa = document.getElementById("<%=classgpa.ClientID%>").value;  
   
            
            if((Valgpa != '') && (Valclassgpa == ''))
            {alert("1");
                if(parseFloat(Valgpa))
                {alert("2");
                alert('Please enter the highest GPA in class');
                ValidatorEnable(document.getElementById('<%=required_classgpa.ClientID%>'),true); 
                document.form2.classgpa.focus();
                }
                else
                {
                document.getElementById("<%=gpa.ClientID%>").value = '';
                }
                return false;
            }
            
            if ((Valclassgpa != '') && (Valgpa == ''))
            {//alert("3");
                if(parseFloat(Valclassgpa))
                {
               // alert("4");
                alert('Please enter the GPA');
                 ValidatorEnable(document.getElementById('<%=required_gpa.ClientID%>'),true); 
                document.form2.gpa.focus();
                }
                else
                {
                document.getElementById("<%=classgpa.ClientID%>").value = '';
                }
                return false;
            }
            
            if((Valgpa != '') && (Valclassgpa != '') )
            {
              if (parseFloat(Valgpa) && parseFloat(Valclassgpa))
                {
                 if (Valgpa > Valclassgpa)
                     {
                      alert('GPA should be less than or equal to class highest GPA');
                      document.form2.classgpa.focus();
                      ValidatorEnable(document.getElementById('<%=compare_gpa.ClientID%>'),true); 
                      return false;
                    }  
                }
            }
            
            if ((Valgpa == '') && (Valclassgpa == '')) 
            { 
              ValidatorEnable(document.getElementById('<%=required_classgpa.ClientID%>'),false); 
              ValidatorEnable(document.getElementById('<%=required_gpa.ClientID%>'),false); 
                   
             }  

             
            //Class rank and total rank
            var Valrank = document.getElementById("<%=classrank.ClientID%>").value;  
            var Valtotalclass = document.getElementById("<%=totalclass.ClientID%>").value;  
   
            if((Valrank != '') && (Valtotalclass == ''))
            {
           alert('ges here');
                    if(parseInt(Valrank))
                    {
                    alert('Please enter a valid number for total class rank');
                    ValidatorEnable(document.getElementById('<%=required_totalclass.ClientID%>'),true); 
                    document.form2.totalclass.focus();
                    }
                    else
                    {
                    document.getElementById("<%=classrank.ClientID%>").value = '';
                    }
            return false;
            }
            
            if ((Valtotalclass != '') && (Valrank == ''))
            {
                    if(parseInt(Valtotalclass))
                    {
                    alert('Please enter a valid number for class rank');
                    ValidatorEnable(document.getElementById('<%=required_classrank.ClientID%>'),true); 
                    document.form2.classrank.focus();
                    }
                    else
                    {
                    document.getElementById("<%=totalclass.ClientID%>").value = '';
                    }
            return false;
            }
            if((Valrank != '') && (Valtotalclass != '') )
            {
              if (parseInt(Valrank) && parseInt(Valtotalclass))
                {
                 if (Valrank > Valtotalclass)
                     {
                      alert('Class rank has to be equal to or lower than the total class rank');
                      ValidatorEnable(document.getElementById('<%=compare_rank.ClientID%>'),true); 
                      document.form2.classrank.focus();
                      return false;
                    }  
                }
            }
            
            if ((Valrank == '') && (Valtotalclass == '')) 
            {  
              ValidatorEnable(document.getElementById('<%=required_totalclass.ClientID%>'),false); 
              ValidatorEnable(document.getElementById('<%=required_classrank.ClientID%>'),false); 
            }  
return true;
        }  
      
      
      function disable_validations()
      {alert("disable");
      
            var Valgpa = document.getElementById("<%=gpa.ClientID%>").value;  
            var Valclassgpa = document.getElementById("<%=classgpa.ClientID%>").value;
            
              if ((Valgpa == '') && (Valclassgpa == '')) 
             {  alert("1");
             ValidatorEnable(document.getElementById('<%=required_classgpa.ClientID%>'),false); 
             ValidatorEnable(document.getElementById('<%=required_gpa.ClientID%>'),false); 
             }
             else
             {
               if ((Valgpa != '') && (Valclassgpa == '')) 
               {
              ValidatorEnable(document.getElementById('<%=required_classgpa.ClientID%>'),true); 
              alert('Please enter the highest GPA in class');
              }
              if ((Valgpa == '') && (Valclassgpa != '')) 
              {
             ValidatorEnable(document.getElementById('<%=required_gpa.ClientID%>'),true);
             alert('Please enter the GPA');
             }
            //  return false;
            
             }
             
             
            var Valrank = document.getElementById("<%=classrank.ClientID%>").value;  
            var Valtotalclass = document.getElementById("<%=totalclass.ClientID%>").value;  
              
            
           if ((Valrank == '') && (Valtotalclass == '')) 
           {  alert("2");
             ValidatorEnable(document.getElementById('<%=required_totalclass.ClientID%>'),false);            
             ValidatorEnable(document.getElementById('<%=required_classrank.ClientID%>'),false);
           }
           else
           {
            if ((Valrank != '') && (Valtotalclass == '')) 
           {
           ValidatorEnable(document.getElementById('<%=required_totalclass.ClientID%>'),true);       
           alert('Please enter a valid number for class rank');
           }
           if ((Valrank == '') && (Valtotalclass != '')) 
           {     
             ValidatorEnable(document.getElementById('<%=required_classrank.ClientID%>'),true);
             alert('Please enter a valid number for total class rank');
             }
        //     return false;
           }
           
return true;
      }
</script>

</head>
<body>

		
	
	<div id="middle">
    	<div id="menu"> 
              <asp:Label ID="errmsg" runat="server" Text="" Visible="false" Enabled="false" Font-Bold="true" ForeColor="Red"></asp:Label>
        </div>
	
	    <div id="main">
		 <div id="form">
                <form id="form2" runat="server">
                  <asp:Panel ID="Panel1"  runat="server" >
                     <asp:ValidationSummary ID="ValidationSummary1" runat="server" 
                      Font-Names="Arial" Font-Size="Small" 
                      HeaderText="Please correct the following errors." ShowMessageBox="true" 
                      ShowSummary="false" ValidationGroup="Submit" />
                     <input type = "hidden" name = "source" value = "W"/>
                     <h2>Student Information</h2>
                         <table style="width:95%; height:100%">
                          
                                  <tr>
                                     <td style="width:40%">Title:</td>
                                     <td valign="top">
                                       <asp:RadioButtonList ID="Sex" runat="server" AppendDataBoundItems="true" CellPadding="0" CellSpacing="0" RepeatDirection="Horizontal" RepeatLayout="Flow" class="nonbox">
                                             <asp:ListItem Value="2">Mr.</asp:ListItem>
                                             <asp:ListItem Value="1">Ms.</asp:ListItem>
                                       </asp:RadioButtonList>
                                       <asp:RequiredFieldValidator ID="RequiredFieldValidator_Sex" runat="server" 
                                             ErrorMessage="Please select a title" Display="Dynamic" 
                                             ControlToValidate="Sex" SetFocusOnError="True" ValidationGroup="Submit">*</asp:RequiredFieldValidator>
                                     </td>
                                  </tr>
                          
                                  <tr>
                                    <td >First Name:</td>
                                    <td><asp:TextBox ID="first" runat="server" Width="225px"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator_first" runat="server" ErrorMessage="Please enter your first name" 
                                              ControlToValidate="first"  Display="Dynamic" SetFocusOnError="True" 
                                            ValidationGroup="Submit">*</asp:RequiredFieldValidator>
                                     </td>
                                  </tr>
                          
                         </table>
                        
                       
      <h2>I am interested in:</h2>
                         <table style="width:90%; height:100%">
                        
                         <tr>
                                <td><h3>Additional Information</h3>
                              
                                     <asp:CheckBoxList ID="additionalinfo" runat="server" AppendDataBoundItems="True" 
                                            CellPadding="0" CellSpacing="0" RepeatLayout="Flow" AutoPostBack="true"
                                      onselectedindexchanged="additionalinfo_SelectedIndexChanged" CausesValidation="false">
                                    
                                     <asp:ListItem Value="256" Text="Honors Program" />
                                     </asp:CheckBoxList>
                                    
                         <asp:Panel ID="Panel2" runat="server" Visible="false" >
                             
                            
	                        <h3>GPA</h3>
	                        <!--GPA and class rank details-->
	                         <table style="width:95%;height:100%;border:0" width="100%">
	                                 <tr>
	                                        <td style="border:0">If weighted, check here&nbsp;<asp:CheckBox 
                                                    ID="weightedGPA" runat="server" oncheckedchanged="weightedGPA_CheckedChanged" AutoPostBack="true" /> </td>
	                                 </tr>
	                                 <tr>
	                                        <td style="border:0">GPA&nbsp;
                                                <asp:TextBox ID="gpa" runat="server" Width="60px"></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="required_gpa" runat="server" 
                                                    ControlToValidate="gpa" Display="Dynamic" 
                                                    Enabled="false"  ErrorMessage="Please enter the GPA"
                                                    SetFocusOnError="true" ValidationGroup="Submit"></asp:RequiredFieldValidator><br />
                                                <asp:RangeValidator ID="validator_gpa" runat="server" ErrorMessage="Please enter a valid number for GPA" ControlToValidate="gpa" MinimumValue="0" MaximumValue="10" Type="Double" SetFocusOnError="True"></asp:RangeValidator>    
                                            </td>
                                       </tr>
                                       <tr>
                	                        <td style="border:0"> Highest Possible GPA in your class 
                                                <asp:TextBox ID="classgpa" runat="server" Width="70px"></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="required_classgpa" runat="server" Enabled="false"
                                                    ControlToValidate="classgpa" ErrorMessage="Please enter the highest GPA in the class"
                                                     SetFocusOnError="true" ValidationGroup="Submit"></asp:RequiredFieldValidator><br />
                                                <asp:RangeValidator ID="validator_classgpa" runat="server" Display="Dynamic" 
                                                    ErrorMessage="Please enter a valid number for GPA" ControlToValidate="classgpa" 
                                                    MinimumValue="0" MaximumValue="10" Type="Double" SetFocusOnError="True" 
                                                    ValidationGroup="Submit"></asp:RangeValidator><br />
                                                <asp:CompareValidator ID="compare_gpa" runat="server" ErrorMessage="GPA should be less than or equal to class highest GPA"
                                                 ControlToValidate="classgpa" Operator="GreaterThanEqual" Type="Double" ValidationGroup="Submit" Display="Dynamic" 
                                                    Enabled="false" ControlToCompare="gpa" SetFocusOnError="True"></asp:CompareValidator> 
                                                  
	                                        </td>
	                                 </tr>
	                                 
	                                 <tr>
	                                         <td style="border:0">Class rank&nbsp; 
                                                <asp:TextBox ID="classrank" runat="server" Width="50px"></asp:TextBox>
                                                 &nbsp; of &nbsp;
                                                <asp:TextBox ID="totalclass" runat="server" Width="60px"></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="required_classrank" runat="server" ErrorMessage="Please enter a valid number for class rank" 
                                                  ControlToValidate="classrank" Enabled="false" SetFocusOnError="True" ValidationGroup="Submit" >*</asp:RequiredFieldValidator>
                                                <br />
                                                <asp:RequiredFieldValidator ID="required_totalclass" runat="server" ErrorMessage="Please enter a valid number for total class rank"
                                                  ControlToValidate="totalclass" Enabled="false" SetFocusOnError="True" ValidationGroup="Submit" >*</asp:RequiredFieldValidator><br />
                                                <asp:RangeValidator ID="classrank_range" runat="server" 
                                                     ErrorMessage="Please enter a valid number value for class rank." ControlToValidate="classrank"  
                                                     MaximumValue="10000" MinimumValue="0" Type="Double"></asp:RangeValidator><br />
                                                 <asp:RangeValidator ID="totalrank1_range" runat="server" ErrorMessage="Please enter a valid number value for the total rank." ControlToValidate="totalclass" 
                                                     MaximumValue="10000" MinimumValue="0" Type="Double" ></asp:RangeValidator><br />
                                                  <asp:CompareValidator ID="compare_rank" runat="server" ErrorMessage="Class rank has to be equal to or lower than the total class rank"
                                                 ControlToValidate="totalclass" Operator="GreaterThanEqual" Type="Integer" ValidationGroup="Submit"
                                                    Enabled="false" ControlToCompare="classrank" SetFocusOnError="True"></asp:CompareValidator>    
                                             </td>
                                     </tr>
                                     
                                       
	        </table>
        	       
	                    
                             
                       
                            
                           
        	            </asp:Panel>
                        
                         </td>
                         </tr>
                        </table>
                         
                       
                         <table style="width:30%; height:100%">
                            <tr>
                                <td>
                                    <asp:Button ID="Submit" runat="server" Text="Submit Form" 
                                        onclick="Submit1_Click"  ValidationGroup="Submit" OnClientClick="if(Page_ClientValidate()) {Check();} else {disable_validations();}" />
                                
                              
                                        
                                </td>
                                
                             </tr>
                        </table> 
                   </asp:Panel>
                </form>
                
            </div>
        <!--stopindex-->
	    </div>
	    
	
	    
	    
	   
	
	  

</div>
    
</body>
</html>

the code behind page is

public partial class WebForm5 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Submit1_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                   Response.Redirect("http://www.google.com");
            }
            else
            {
                errmsg.Enabled = true;
                errmsg.Visible = true;
                errmsg.Text = "Errors";
               

            }
        }
        protected void additionalinfo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (additionalinfo.Items[0].Selected == true)
            {
                Panel2.Visible = true;
                SetFocus(weightedGPA);
            }
            else
            {
                Panel2.Visible = false;
                SetFocus(Submit);
            }
        }
        protected void weightedGPA_CheckedChanged(object sender, EventArgs e)
        {
            
            if (weightedGPA.Checked == true)
            {

                required_gpa.Enabled = true;
                required_gpa.SetFocusOnError = true;

                required_classgpa.Enabled = true;
                required_classgpa.SetFocusOnError = true;

            }
            else
            {
                required_gpa.Enabled = false;
                required_classgpa.Enabled = false;

            }


        }

    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.