Solved

Custom validator for asp.net checkbox list

Posted on 2012-04-05
4
737 Views
Last Modified: 2012-04-16
I am using a custom validator for my radiobuttonlist, with a code simmilar to the one below, but I switched the radiobuttonlist to a checkboxlist, and seems to not work, the purpose is to check if the user selected one option that says ' other' have him enter data in the other textbox, can you please suggest how to make it work?

                 <asp:TextBox ID="txt2other" runat="server" CssClass="txtOther"></asp:TextBox>
                       &nbsp;<asp:CustomValidator ID="CustomValidator3" runat="server"
                           ClientValidationFunction="checkOther2" ControlToValidate="txt2other"
                           ErrorMessage="Please enter a comment in question #2." style="font-weight: 700"
                           ValidateEmptyText="True"></asp:CustomValidator>


        function checkOther2(oSrc, args) {
            var rbtnValue = null;
            var rbtnList = document.getElementsByName('<%= CheckBoxList1.ClientID %>');
            var radio = rbtnList[0].getElementsByTagName("input");
            for (var j = 0; j < radio.length; j++) {
                if (radio[j].checked)
                    rbtnValue = radio[j].value;
            }
            if (rbtnValue == 'Other') {
                args.IsValid = !(args.Value == "")
            }
            else {
                args.IsValid = true;
            }
        }
0
Comment
Question by:TonyReba
  • 2
  • 2
4 Comments
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
Comment Utility
0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
Can you check the code above and see if any error??
0
 
LVL 20

Expert Comment

by:BuggyCoder
Comment Utility
controltovalidate should point to the checkboxlist rather than text box
0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
Sorry , what I need is to validate the textbox, but is required only if

 if (rbtnValue == 'Other')  



Is selectected.....
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now