javascript - how to make items required when checkbox is ticked? -


i trying set textbox, , dropdownlist , textbox datepicker required if checkbox ticked. if checkbox not ticked fields not required, if ticked fields required.

<table>     <tbody>     <tr>         <td class="header">haz trained?</td>         <td></td>         <td>             <asp:checkbox runat="server" id="chkhaztrained" onclick="updatevalidator();"/>         </td>     </tr>     <tr>         <td class="header">haz license no</td>         <td></td>         <td>             <asp:textbox runat="server" id="txthazlicenseno"></asp:textbox>             <asp:requiredfieldvalidator id="rfvhazlicenseno" controltovalidate="txthazlicenseno" errormessage="you must enter license no." runat="server" />          </td>     </tr>     <tr>         <td class="header">license type</td>         <td></td>         <td>             <asp:dropdownlist runat="server" id="ddlhazlicensetype">             </asp:dropdownlist>          </td>     </tr>      <tr>         <td class="header">expiry date         </td>                                                    <td></td>         <td>             <asp:textbox runat="server" id="txthazlicenseexpirydate" rel="datepicker" cssclass="dateonly"></asp:textbox>          </td>     </tr>                          </tbody> </table> 

i tried creating javascript function doesn't work right. if checkbox not ticked, ask textbox filled. if checkbox not ticked other fields not required. how can fix problem?

function updatevalidator() {         var enablevalidator = !event.srcelement.status;         var rfvhazlicenseno = document.getelementbyid('rfvhazlicenceno');         validatorenable(rfvhazlicenseno, enablevalidator);     } 

tried customervalidator:

     <asp:textbox runat="server" id="txthazlicenseno" text=""></asp:textbox>                                                         <asp:customvalidator id="customvalidator2" runat="server"                                                            controltovalidate = "txthazlicenseno"                                                           errormessage = "please enter"                                                           clientvalidationfunction="validatehazlicence" >                                                         </asp:customvalidator>  function validatehazlicence(osrc, args){         if(chkhaztrained.checked == true)         {             args.isvalid = (args.value.length > 0);         }     } 

but when checkbox ticked doesn't make textbox required.

i believe want use custom validator. textbox/ddl valid if chkhaztrained.checked == false, or if chkhaztrained.checked == true , textbox/ddl has value.

i'd imagine work using requiredfieldvalidators , updating .enabled property on checkbox's checkedchanged event, too, require checkbox postback.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -