I am trying to set a textbox, and dropdownlist and a textbox with a datepicker to required if a checkbox is ticked. So if checkbox is not ticked then the fields are not required, but if it is ticked all fields are 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 a 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 a javascript function but it doesn't work right. Even if checkbox is not ticked, it will ask for textbox to be filled. If checkbox is not ticked then other fields are not required. So how can I fix this 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 is ticked it doesn't make textbox required.