Solved

Custom validator for asp.net checkbox list

Posted on 2012-04-05
4
745 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37813797
0
 
LVL 9

Author Comment

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

Expert Comment

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

Author Comment

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

 if (rbtnValue == 'Other')  



Is selectected.....
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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