Validate ASP.Net CheckBoxList and TextBox based on condition using JavaScript

Last Reply one month ago By dharmendr

Posted one month ago

Thank you for providing a very good example how to validate checkboxlist, Validate ASP.Net CheckBoxList (at least one CheckBox checked) using Custom Validator

It works great!

I would like to have  another selection "Other" to the <asp:CheckBoxList">  and a textbox. If the user selects "Other" option then he/she must to fill out the textbox.  How would we do that? Thank so much for your help.

The textbox doesn't need to be visible. Thanks so much for your help.

Select Fruits<br />
<asp:CheckBoxList ID="chkFruits" runat="server">
    <asp:ListItem Text="Mango" />
    <asp:ListItem Text="Apple" />
    <asp:ListItem Text="Banana" />
    <asp:ListItem Text="Pineapple" />
    <asp:ListItem Text="Guava" />
    <asp:ListItem Text="Grapes" />
    <asp:ListItem Text="Papaya" />
    <asp:ListItem Text="Other" />
</asp:CheckBoxList>
<asp:TextBox id="txtOtherfruit" rows="5" TextMode="multiline"
runat="server" />
<asp:CustomValidator ID="CustomValidator1" ErrorMessage="Please select at least one item."
    ForeColor="Red" ClientValidationFunction="ValidateCheckBoxList" runat="server" />
<br />
<br />
<asp:Button ID="btnSubmit" Text="Submit" runat="server" /><br />

 

Posted one month ago

Hi Thuyai,

Check this example. Now please take its reference and correct your code.

HTML

Select Fruits<br />
<asp:CheckBoxList ID="chkFruits" runat="server">
    <asp:ListItem Text="Mango" />
    <asp:ListItem Text="Apple" />
    <asp:ListItem Text="Banana" />
    <asp:ListItem Text="Pineapple" />
    <asp:ListItem Text="Guava" />
    <asp:ListItem Text="Grapes" />
    <asp:ListItem Text="Papaya" />
    <asp:ListItem Text="Other" />
</asp:CheckBoxList>
<asp:CustomValidator ID="CustomValidator1" ErrorMessage="Please select at least one item."
    ForeColor="Red" ClientValidationFunction="ValidateCheckBoxList" runat="server" />
<br />
<asp:TextBox ID="txtOtherfruit" Rows="5" TextMode="multiline" runat="server" />
<asp:RequiredFieldValidator ID="rfvOtherFruit" ErrorMessage="Required" ControlToValidate="txtOtherfruit"
    Enabled="false" runat="server" ForeColor="Red" />
<br />
<br />
<asp:Button ID="btnSubmit" Text="Submit" runat="server" /><br />
<script type="text/javascript">
    function ValidateCheckBoxList(sender, args) {
        var checkBoxList = document.getElementById("<%=chkFruits.ClientID %>");
        var checkboxes = checkBoxList.getElementsByTagName("input");
        var isValid = false;
        for (var i = 0; i < checkboxes.length; i++) {
            if (checkboxes[i].checked) {
                isValid = true;
                if (checkboxes[i].value == 'Other') {
                    ValidatorEnable(document.getElementById("rfvOtherFruit"), true);
                    break;
                }
                break;
            }
        }
        args.IsValid = isValid;
    }
</script>

Screenshot

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html