locked
disable one requiredfieldvalidator based on the other required field validator selection.-same validator group RRS feed

  • Question

  • User1765346662 posted

    The function that makes sure atleast one test is selected, but I want to add a text field so that if any text in entered in that field the tdpooling and tdindivials validators are disabled...

    I tried the code that I highlighted but its not working...

    function validatorsForTestInfo() {
    if (Page_ClientValidate('vGrpPreview')) {
    var hasASelection = false;
    var cntChkBoxesChecked = 0;
    $('#ctl00_ContentPlaceHolder1_tblTest>tbody>tr').each(function () {
    var chkBox = $(this).find('input[type="checkbox"]');
    if (chkBox.length) { //loops through the rows that has checkboxes(that has tests)
    if (chkBox.prop('checked')) { //if the check box is checked then check either indi or pool is selected
    cntChkBoxesChecked++; //count number of check boxes checked
    var tdIndividual = $(this).find('td:eq(1) input').attr('id');
    var tdPooling = $(this).find('td:eq(3) input').attr('id');
    var tdIndividualValue = $(this).find('td:eq(1) input').val();// 9/1 *WIP*
    var tdPoolingValue = $(this).find('td:eq(3) input').val();
    var spanRequiredFieldId = $(this).find('td:eq(1) span').attr('id');
    if (tdIndividualValue) { //check if the individual textbox has value
    ValidatorEnable(document.getElementById(spanRequiredFieldId), false);

    var CustomValidator2 = document.getElementById("<%=CustomValidator2.ClientID%>");
    ValidatorEnable(CustomValidator2, false);


    hasASelection = true;
    }
    else if (tdPooling != 'undefined' && tdPoolingValue) { //first check if the test has pooling textbox, if exsists then check if the textbox has a value
    ValidatorEnable(document.getElementById(spanRequiredFieldId), false);
    var CustomValidator2 = document.getElementById("<%=CustomValidator2.ClientID%>");
    ValidatorEnable(CustomValidator2, false);
    hasASelection = true;
    }
    else if (!tdIndividualValue && tdPooling != 'undefined' && !tdPoolingValue) //if both individual & pooling textbox values are empty then display error msg
    { //check box checked but not selected either individual or pooling, hence enable required field validators for individual & pooling
    <%--if (document.getElementById('<%=txtBacteriologyText.ClientID%>').value != '') {
    alert("part1");
    ValidatorEnable(document.getElementById(vGrpPreview), false);
    $("#ctl00_ContentPlaceHolder1_divTestSelectionMsg").hide();
    hasASelection = true;
    return true; //comes out of for each row loop
    }
    else {--%>
    ValidatorEnable(document.getElementById(spanRequiredFieldId), true);
    alert("part2");
    hasASelection = false;
    return false; //comes out of for each row loop

    }
    }
    }
    });
    if (cntChkBoxesChecked == 0) {
    alert("part3");
    $("#ctl00_ContentPlaceHolder1_divTestSelectionMsg").show();
    $(window).scrollTop($('#ctl00_ContentPlaceHolder1_divTestSelectionMsg').offset().top);
    hasASelection = false;
    }
    if (hasASelection == true) {
    alert("part4");
    $("#ctl00_ContentPlaceHolder1_divTestSelectionMsg").hide();
    SavingSampleMetaData(); //if the tests table passed all validations then save the data in hiddenfield
    }
    return hasASelection;
    }
    //else if (Page_ClientValidate('vGrpBVTest')) {
    // ValidatorEnable(ValidatorContronName,false);

    //}
    }

    This is the validator piece I want to disable when a test is selected:

    <div class="tbl_heading">BACTERIOLOGY</div>
    <div class="tbl_dummy"></div>
    <div class="row form-group form-group-sm">
    <div class="col-xs-2">
    <asp:Label ID="lblBacteriologyText" CssClass="control-label" runat="server" Text="Bacteriology Test Details:"></asp:Label>
    </div>
    <div class="col-xs-4">
    <asp:TextBox ID="txtBacteriologyText" aria-label="Bacteriology Text" runat="server" CssClass="form-control" MaxLength="500" TextMode="MultiLine"></asp:TextBox>
    <%--<asp:CustomValidator ID="CustomValidator2" runat="server"
    ValidateEmptyText="true" Display="Dynamic" ClientValidationFunction="validatorforBactiTextBox"
    ValidationGroup="vGrpBactiVirology" /> --%>
    <asp:RequiredFieldValidator ID="CustomValidator2" ControlToValidate="txtBacteriologyText" runat="server"
    ErrorMessage="*Required" ForeColor="Red" ValidationGroup="vGrpPreview" />
    </div>
    </div>

    The code for Submit button: 

    <div class="row form-group form-group-sm">
    <div class="col-xs-12">
    <asp:Button ID="btnPreview" runat="server" Text="Submit Preview"
    OnClientClick="return validatorsForTestInfo()" OnClick="btnPreview_Click"
    ValidationGroup="vGrpPreview" CssClass="btn-clinic btn-clinic-md" />
    </div>
    </div>

    Friday, August 30, 2019 5:06 PM

All replies

  • User288213138 posted

    Hi shivali167,

    Since your code is incomplete, I cannot fully reproduce your problem,

    but according to your description, I made a demo for you.

    In jquery I use ValidatorEnable() to disable the RequiredFieldValidator control.

    The code:

    <script>
            $(function () {
                $("#btnPreview").click(function () {
                    var t = $("#txtBacteriologyText").val().trim();
                    if (t == "open") {
                        ValidatorEnable(document.getElementById('<%=RequiredFieldValidator2.ClientID %>'), false);
                 
                    }
                })
    
            })
    </script>
    <div class="tbl_heading">BACTERIOLOGY</div>
            <div class="tbl_dummy"></div>
            <div class="row form-group form-group-sm">
                <div class="col-xs-2">
                    <asp:Label ID="lblBacteriologyText" CssClass="control-label" runat="server" Text="Bacteriology Test Details:"></asp:Label>
                </div>
                <div class="col-xs-4">
    
    
                    Name:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ForeColor="Red" ErrorMessage="*Required"  ControlToValidate="TextBox2" ValidationGroup="vGrpPreview"></asp:RequiredFieldValidator><br />
                Date:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ForeColor="Red" ErrorMessage="*Required"  ControlToValidate="TextBox3" ValidationGroup="vGrpPreview"></asp:RequiredFieldValidator><br />
    
                    <asp:TextBox ID="txtBacteriologyText" aria-label="Bacteriology Text" runat="server" CssClass="form-control" MaxLength="500" TextMode="MultiLine"></asp:TextBox>           
                    <asp:RequiredFieldValidator ID="CustomValidator2" ControlToValidate="txtBacteriologyText" runat="server"
                        ErrorMessage="*Required" ForeColor="Red" ValidationGroup="vGrpPreview" />
                </div>
            </div>
            <div class="row form-group form-group-sm">
                <div class="col-xs-12">
                    <asp:Button ID="btnPreview" runat="server" Text="Submit Preview"
                         OnClick="btnPreview_Click"
                        ValidationGroup="vGrpPreview" CssClass="btn-clinic btn-clinic-md" />
                </div>
            </div>

    The result:

    you can also refer to this link:https://forums.asp.net/t/2158412.aspx

    Best regards,

    Sam

    Monday, September 2, 2019 8:44 AM