asp.net - Validate RadioButton List Using Javascript and CustomValidator -
i using customvalidator radiobuttonlist,instead of requiredfieldvalidator, can assign css class using javascript.the validation check @ least 1 of items selected. foll. code not working:
js:
function validateradiobuttonlist() { var target = document.getelementbyid('<%=rbgender.clientid %>'); var radiobuttons = target.getelementsbytagname('input'); var is_valid; if (radiobuttons[0].checked || radiobuttons[1].checked) { is_valid = true; target.classname = ""; } else { target.classname = "validate"; is_valid = false; } args.isvalid = is_valid; }
aspx:
<asp:radiobuttonlist id="rbgender" runat="server" repeatdirection="horizontal" repeatlayout="flow" autopostback="false"> <asp:listitem>male </asp:listitem> <asp:listitem>female </asp:listitem> </asp:radiobuttonlist> <asp:customvalidator id="customvalidator3" runat="server" controltovalidate="rbgender" clientvalidationfunction="validateradiobuttonlist" text="*" forecolor="red"> </asp:customvalidator> <asp:button id="btnupdate" runat="server" text="update" validationgroup="vsgroup" />
generated html:
<div class=""> <span id="contentplaceholder1_rbgender"> <input id="contentplaceholder1_rbgender_0" type="radio" name="ctl00$contentplaceholder1$rbgender" value="male " /> <label for="contentplaceholder1_rbgender_0">male </label> <input id="contentplaceholder1_rbgender_1" type="radio" name="ctl00$contentplaceholder1$rbgender" value="female " /> <label for="contentplaceholder1_rbgender_1">female </label> </span> <span style="float: right; margin-right: 3%"> <span id="contentplaceholder1_customvalidator3" style="color:red; visibility:hidden;">* </span> </span> </div>
but if include
usesubmitbehavior="false" onclientclick="validateradiobuttonlist()"
for button, works. not want so.
try without asp validators. can solved using javascript. try link reference: http://www.felgall.com/javatip2.htm
Comments
Post a Comment