We help IT Professionals succeed at work.
Get Started

Custom Validation problem for website in Visual Studio

Last Modified: 2013-11-26
I currently have a custom validator on one of my fields in my website that uses a javascript function.  The function looks at my txtOthHrs field and if the value is not equal to zero then it displays a warning message under my DD_OthHourType field. (In non-technical terms if a user keys in any value for my other hours field other than zero then they must select an other hours type from a dropdown).  I have two problems with how my code works.  

1. If a user keys in a value <> 0 and then hits Tab or Enter the warning message appears (this is what I want).  However, if the user changes the value to zero and hits Tab or Enter again the message does not disappear.  How do I get it so that anytime the user changes the value of the txtOthHrs the javascript function runs again?

2. I have an Add button under these fields that when clicked adds the record to the database.  I need to put code on the Addbutton_click event so that it validates that if the txtOthHrs field value is not equal to zero the selected index of othHrsType dropdown list must not be equal to zero.  Meaning that if a user entered any value in my other hours field other than zero they must select a other hours type from the dropdown.
Javascript function:
<script language="javascript" type="text/javascript">
    function Validate(source,arguments)
                if ((
                    document.getElementById("<%=txtOthHrs.ClientID%>").value == 0
                ) && (
                    document.getElementById("<%=DD_OthHourType.ClientID %>").selectedIndex = 0
                    arguments.IsValid = true                          
                    arguments.IsValid = false;
txtOthHrs and DD_OthHrsType code:
<td class="table2_data" style="height: 31px" >
                    <asp:TextBox ID="txtOthHrs" runat="server" Width="93%" TabIndex="10" style="text-align: right">0</asp:TextBox><br />
                    <asp:RequiredFieldValidator ID="ReqValOTHrs" runat="server" ControlToValidate="txtOthHrs"
                        Display="Dynamic" ErrorMessage="This field must have a value; if none, enter 0"
                        SetFocusOnError="True" ValidationGroup="FormValidate"></asp:RequiredFieldValidator>
                <td style="height: 31px" >
                    <asp:DropDownList ID="DD_OthHourType" runat="server" DataSourceID="OtherHoursType"
                        DataTextField="Desc" DataValueField="OthHoursCode" TabIndex="11" Width="100%" style="text-align: right" AppendDataBoundItems="True" CausesValidation="True">
                    <asp:CustomValidator ID="cvOthHrsType" runat="server" ControlToValidate="txtOthHrs" ErrorMessage="Please Select an Other Hours Type" ClientValidationFunction="Validate" Display="Dynamic" ValidationGroup="FormValidate" SetFocusOnError="True"></asp:CustomValidator></td>

Open in new window

Watch Question
This problem has been solved!
Unlock 2 Answers and 23 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE