how to get a value in a list on a checkboxlist control and use it to validatehttp://forums.asp.net/t/1789778.aspx/1?how+to+get+a+value+in+a+list+on+a+checkboxlist+control+and+use+it+to+validateFri, 06 Apr 2012 06:55:49 -040017897784918753http://forums.asp.net/p/1789778/4918753.aspx/1?how+to+get+a+value+in+a+list+on+a+checkboxlist+control+and+use+it+to+validatehow to get a value in a list on a checkboxlist control and use it to validate <p>I have a form in asp.net 3.5 , which has a master page and a child page (content page). The form has several questions, I am using the asp.net checkboxlist for a questions, since multiple options can be selected, if the user selects 'Other' on one of the selections, then they have to enter data into a textbox field.</p> <p>I made the following client side javascript to validate this but, the code doesnt seem to check wheter the Other option value is selected, I believe it has to do with the way the html is rendered on the page,,,</p> <p>Can you please suggest how to do this?</p> <p>Thanks in advance.</p> <p>&nbsp;</p> <pre class="prettyprint">I have a form in asp.net 3.5 , which has a master page and a child page (content page). The form has several questions, I am using the asp.net checkboxlist for a questions, since multiple options can be selected, if the user selects 'Other' on one of the selections, then they have to enter data into a textbox field. I made the following client side javascript to validate this but, the code doesnt seem to check wheter the Other option value is selected, I believe it has to do with the way the html is rendered on the page,,, Can you please suggest how to do this? Thanks in advance. Rendered Javascript //Here I am trying to get the text property of the label rendered for the texbox // and set my validation arguments &lt;script language='javascript' type='text/javascript'&gt; function checkOther2(oSrc, args) { { var elementRef = document.getElementById('ctl00_Content_CheckBoxList1'); var checkBoxArray = elementRef.getElementsByTagName('input'); var checkedValues = ''; for (var i = 0; i &lt; checkBoxArray.length; i&#43;&#43;) { var checkBoxRef = checkBoxArray[i]; if (checkBoxRef.checked == true) { // You can only get the Text property, which will be in an HTML label element. var labelArray = checkBoxRef.parentNode.getElementsByTagName('label'); if (labelArray.length &gt; 0) { if (checkedValues.length &gt; 0) checkedValues &#43;= ','; checkedValues &#43;= labelArray[0].innerHTML.text; if (checkedValues == 'Other') { args.IsValid = !(args.Value == &quot;&quot;) // test alert(&quot;Hello&quot;); } } else { args.IsValid = true; } } } } } // HTML Rendered &lt;tr&gt; &lt;td style=&quot;height: 20px&quot;&gt; &amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td style=&quot;font-weight: 700&quot;&gt; 2.- What did you like most about working here?&lt;strong&gt; Check all that apply &lt;span id=&quot;ctl00_Content_CheckBoxListValidator1&quot; style=&quot;color:Red;display:none;&quot;&gt;&lt;/span&gt; &lt;/strong&gt;&lt;br /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;table id=&quot;ctl00_Content_CheckBoxList1&quot; class=&quot;myradioButton&quot; border=&quot;0&quot;&gt; &lt;tr&gt; &lt;td&gt;&lt;input id=&quot;ctl00_Content_CheckBoxList1_0&quot; type=&quot;checkbox&quot; name=&quot;ctl00$Content$CheckBoxList1$0&quot; /&gt;&lt;label for=&quot;ctl00_Content_CheckBoxList1_0&quot;&gt;Staff&lt;/label&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;&lt;input id=&quot;ctl00_Content_CheckBoxList1_1&quot; type=&quot;checkbox&quot; name=&quot;ctl00$Content$CheckBoxList1$1&quot; /&gt;&lt;label for=&quot;ctl00_Content_CheckBoxList1_1&quot;&gt;Facility&lt;/label&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;&lt;input id=&quot;ctl00_Content_CheckBoxList1_2&quot; type=&quot;checkbox&quot; name=&quot;ctl00$Content$CheckBoxList1$2&quot; /&gt;&lt;label for=&quot;ctl00_Content_CheckBoxList1_2&quot;&gt;Pay&lt;/label&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;&lt;input id=&quot;ctl00_Content_CheckBoxList1_3&quot; type=&quot;checkbox&quot; name=&quot;ctl00$Content$CheckBoxList1$3&quot; /&gt;&lt;label for=&quot;ctl00_Content_CheckBoxList1_3&quot;&gt;Other&lt;/label&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; If other, please elaborate:&lt;br /&gt; &lt;input name=&quot;ctl00$Content$txt2other&quot; type=&quot;text&quot; id=&quot;ctl00_Content_txt2other&quot; class=&quot;txtOther&quot; /&gt; &amp;nbsp;&lt;span id=&quot;ctl00_Content_CustomValidator3&quot; style=&quot;color:Red;font-weight:700;visibility:hidden;&quot;&gt;Please enter a comment in question #2.&lt;/span&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td style=&quot;font-weight: 700&quot;&gt; 2.- What did you like most about working here?&lt;strong&gt; Check all that apply &lt;cc1:CheckBoxListValidator ID=&quot;CheckBoxListValidator1&quot; runat=&quot;server&quot; ControlToValidate=&quot;CheckBoxList1&quot; Display=&quot;None&quot; ErrorMessage=&quot;Question 2 is Required&quot;&gt;&lt;/cc1:CheckBoxListValidator&gt; &lt;/strong&gt;&lt;br /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; ----------- Actual Markup on asp.net form &lt;asp:CheckBoxList ID=&quot;CheckBoxList1&quot; runat=&quot;server&quot; CssClass=&quot;myradioButton&quot;&gt; &lt;asp:ListItem Text=&quot;Staff&quot; Value=&quot;Staff&quot;&gt;Staff&lt;/asp:ListItem&gt; &lt;asp:ListItem Text=&quot;Facility&quot; Value=&quot;Facility&quot;&gt;Facility&lt;/asp:ListItem&gt; &lt;asp:ListItem Text=&quot;Pay&quot; Value=&quot;Pay&quot;&gt;Pay&lt;/asp:ListItem&gt; &lt;asp:ListItem Text=&quot;Other&quot; Value=&quot;Other&quot;&gt;Other&lt;/asp:ListItem&gt; &lt;/asp:CheckBoxList&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; If other, please elaborate:&lt;br /&gt; &lt;asp:TextBox ID=&quot;txt2other&quot; runat=&quot;server&quot; CssClass=&quot;txtOther&quot;&gt;&lt;/asp:TextBox&gt; &amp;nbsp;&lt;asp:CustomValidator ID=&quot;CustomValidator3&quot; runat=&quot;server&quot; ClientValidationFunction=&quot;checkOther2&quot; ControlToValidate=&quot;txt2other&quot; ErrorMessage=&quot;Please enter a comment in question #2.&quot; style=&quot;font-weight: 700&quot; ValidateEmptyText=&quot;True&quot;&gt;&lt;/asp:CustomValidator&gt; &lt;/td&gt; &lt;/tr&gt;</pre> <p><br> &nbsp;</p> 2012-04-05T23:42:01-04:004919050http://forums.asp.net/p/1789778/4919050.aspx/1?Re+how+to+get+a+value+in+a+list+on+a+checkboxlist+control+and+use+it+to+validateRe: how to get a value in a list on a checkboxlist control and use it to validate <p>CheckboxList validation using jquery</p> <p><a href="http://weblogs.asp.net/samirgeorge/archive/2009/05/02/checkboxlist-client-side-validation-using-jquery.aspx">http://weblogs.asp.net/samirgeorge/archive/2009/05/02/checkboxlist-client-side-validation-using-jquery.aspx</a></p> <p><a href="http://www.zachhunter.com/2010/06/jquery-validation-with-asp-net/">http://www.zachhunter.com/2010/06/jquery-validation-with-asp-net/</a></p> 2012-04-06T06:55:49-04:00