locked
How to hide asp:CustomValidator's error message? RRS feed

  • Question

  • User-809753952 posted

    I have a check box and a custom validator inside a detailsview EditItemTemplate.  And a submit button outside the detailsview.

    <asp:CheckBox ID="chkIs_Order_Match_Quote" runat="server" ValidationGroup="Group1"
                Checked='<%# Bind("Is_Order_Match_Quote") %>' onkeypress="if (event.keyCode == 13) {event.preventDefault();}"
                onclick="  $(this).closest('td').css('background-color', $(this).is(':checked') ? '#dff0d8' : '#f2dede');" />
            <asp:CustomValidator ID="vTerms" ClientValidationFunction="validateTerms1" ValidateEmptyText="true"
                ErrorMessage="*" Font-Size="10px" CssClass=" glyphicon glyphicon-alert" ForeColor="#D00000"
                Display="Dynamic" EnableClientScript="true" ValidationGroup="Group1" runat="server" />
    
            <asp:Button ID="btnPlanningCompleted" runat="server" Text="Proceed to Implementation"
                CssClass="btn btn-success btn-sm btn-block pull-right " OnClick="PlanningCompleted"
                ValidationGroup="Group1"></asp:Button>
    The validation function is as follows:

       <script type="text/javascript">
            function validateTerms1(source, arguments) {
    
                if ($("#dvCustomerSolution_chkIs_Order_Match_Quote").prop("checked")) {
                    arguments.IsValid = true;
                } else {
                    arguments.IsValid = false;
    
                }
            }
        </script>

    When I click on the button "btnPlanningCompleted" , the error message is shown, if the check box is not checked. 

    But when I check the checkbox, the error message is not hidden. It disappears only when the button , "btnPlanningCompleted" is clicked.

    How can I hide it when the checkbox checked = true?

    Friday, February 2, 2018 2:30 PM

All replies

  • User2103319870 posted

    When I click on the button "btnPlanningCompleted" , the error message is shown, if the check box is not checked. 

    But when I check the checkbox, the error message is not hidden. It disappears only when the button , "btnPlanningCompleted" is clicked.

    How can I hide it when the checkbox checked = true?

    You can try with below implementation.

    Add a function to checkbox click event like below

    <asp:CheckBox ID="chkIs_Order_Match_Quote" runat="server" ValidationGroup="Group1"
                Checked='<%# Bind("Is_Order_Match_Quote") %>' onkeypress="if (event.keyCode == 13) {event.preventDefault();}"
                onclick="  $(this).closest('td').css('background-color', $(this).is(':checked') ? '#dff0d8' : '#f2dede');DisableValidator()" />
    

    Then use the below function to hide validator when checkbox is checked

    function DisableValidator() {
                    //Check if checkbox is checked
                    if ($("#dvCustomerSolution_chkIs_Order_Match_Quote").prop("checked")) {
                        //Hide the validator
                        $("#<%=vTerms.ClientID %>").toggle(false);
                    }
                }

    Friday, February 2, 2018 2:47 PM
  • User-707554951 posted

    Hi Hi mnmhemaj

    Working code as below:

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script type="text/javascript">
            function validateTerms1(source, arguments) {
                if ($("input[id*='chkIs_Order_Match_Quote']").prop("checked")) {
                    arguments.IsValid = true;
                } else {
                    arguments.IsValid = false;
    
                }
            }
            $(document).ready(function () {
                $("input[id*='chkIs_Order_Match_Quote']").change(function () {
                    if ($(this).prop("checked")) {
                        if ($("DetailsView1_vTerms") != null) {
                            $("span[id*='DetailsView1_vTerms']").hide();
                        }
                    }
                })
            })   
        </script>
      <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="false" AutoGenerateEditButton="true" OnModeChanging="DetailsView1_ModeChanging">
                    <Fields>
                        <asp:BoundField DataField="Id" HeaderText="Id" HeaderStyle-Font-Bold="true" />
                        <asp:BoundField DataField="Name" HeaderText="Name" HeaderStyle-Font-Bold="true" />
                        <asp:TemplateField HeaderText="CustomerID" Visible="True" InsertVisible="True">
                            <ItemTemplate>
                                <asp:Label ID="CustomerIDLabel" Text='<%# Eval("Checked") %>' runat="server"></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:CheckBox ID="chkIs_Order_Match_Quote"  runat="server" ValidationGroup="Group1"
                                    Checked='<%#Convert.ToBoolean(Eval("Checked").ToString())%>' onkeypress="if (event.keyCode == 13) {event.preventDefault();}"
                                    onclick=" $(this).closest('td').css('background-color', $(this).is(':checked') ? '#dff0d8' : '#f2dede');DisableValidator()" />
                                <asp:CustomValidator ID="vTerms" ClientValidationFunction="validateTerms1" ValidateEmptyText="true"
                                    ErrorMessage="*" Font-Size="10px" CssClass=" glyphicon glyphicon-alert" ForeColor="#D00000"
                                    Display="Dynamic" EnableClientScript="true" ValidationGroup="Group1" runat="server" />
                            </EditItemTemplate>
                        </asp:TemplateField>
                    </Fields>
                </asp:DetailsView>
                <asp:Button ID="btnPlanningCompleted" runat="server" Text="Proceed to Implementation"
                    CssClass="btn btn-success btn-sm btn-block pull-right "  ValidationGroup="Group1" OnClick="btnPlanningCompleted_Click" />
            </div>
    

    CodeBehind:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    getCustomers();
                }
    
            }
            public void getCustomers()
            {
    
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                        new DataColumn("Name", typeof(string)),
                        new DataColumn("Checked",typeof(string)) });
                dt.Rows.Add(1, "John Hammond", "True");
                dt.Rows.Add(2, "Mudassar Khan", "ASP.Net programmer and consultant in India.");
                DetailsView1.DataSource = dt;
                DetailsView1.DataBind();
            }
    
            protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
            {
                DetailsView1.ChangeMode(e.NewMode);
                getCustomers();
                if (e.CancelingEdit)
                {           
                this.DetailsView1.DefaultMode = DetailsViewMode.ReadOnly;
                    this.DetailsView1.DataBind();
                }
            }
    
            protected void btnPlanningCompleted_Click(object sender, EventArgs e)
            {
    
            }

    Output:

    Best regards

    Cathy

    Monday, February 5, 2018 6:50 AM