CustomValidator runs on the client, but verification control is not displayed when verification is not performed

I have a simple one: if the yes switch is checked, than the text field is required. I performed a check both on the client side and on the server side. I found that:

  • Client validation starts and correctly displays the failure with a warning message.
  • The validation control never displays an error message and the server crashes.
  • When the server crashes and the verification is completed, the control displays the correct error message.

Why does the client work correctly from the point of view of verification, but does not transmit an error message and continues to work on the server?

asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script src="Scripts/jquery-1.7.2.js" type="text/javascript"></script> 

<script type="text/javascript">
    function testClientValidation(src, args) {
        if ($('#<%=rblstTest.ClientID%>' + ' input:checked').length == 1) {
            if ($('#<%=rblstTest.ClientID%>' + ' input:checked').val().toLowerCase() == "yes") {
                args.isValid = !($('#<%=txtTest.ClientID%>').val() == "");  
            } else {
                args.isValid = true;
            }
        } else {
            args.isValid = true;
        }

        alert(args.isValid);
    }
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<div>
<asp:RadioButtonList ID="rblstTest" RepeatDirection="Horizontal" RepeatLayout="Flow" runat="server">
    <asp:ListItem>Yes</asp:ListItem>
    <asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList> 

<asp:TextBox ID="txtTest" runat="server"/>

<asp:CustomValidator ID="cust" ControlToValidate="rblstTest" 
                                OnServerValidate="testSeverValidation" 
                                ClientValidationFunction="testClientValidation" 
                                Display="Dynamic" 
                                ErrorMessage="Error!" runat="server"/>

 <asp:LinkButton runat="server" ID="lnkSubmit" Text="Submit"  />
 </div>  
 </asp:Content>
+3
source share
2 answers

args.IsValid args.IsValid

function testClientValidation(src, args) {
        if ($('#<%=rblstTest.ClientID%>' + ' input:checked').length == 1) {
            if ($('#<%=rblstTest.ClientID%>' + ' input:checked').val().toLowerCase() == "yes") {
                args.IsValid = !($('#<%=txtTest.ClientID%>').val() == "");  
            } else {
                args.IsValid = true;
            }
        } else {
            args.IsValid = true;
        }

        alert(args.IsValid);
    }
+4

Hidden Field.

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<script type="text/javascript" language="javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript" language="javascript" src="Scripts/jquery-1.4.1.js"></script>
<script language="javascript" type="text/javascript">

    function testClientValidation(src, args) {
        debugger;
        if (document.getElementById('<%= hdn.ClientID %>').value == '1') {
            var txtBox = document.getElementById('<%= txtTest.ClientID%>');
            if (txtBox.value == '') {
                document.getElementById('cust').style.display = 'block';
                document.getElementById('cust').innerHTML = 'Error!'
                document.getElementById('<%= hdn.ClientID %>').value = "1";
                args.IsValid = false;
                return false;
            }
            else {
                document.getElementById('cust').style.display = 'none';
                document.getElementById('cust').innerHTML = '';
                document.getElementById('<%= hdn.ClientID %>').value = "0";
                args.IsValid = true;
            }
        }
        else {
            document.getElementById('cust').style.display = 'none';
            document.getElementById('cust').innerHTML = '';
            document.getElementById('<%= hdn.ClientID %>').value = "0";
            args.IsValid = true;
        }
        return true;
    }


    $(document).ready(function () {
        $("span input[type='radio']").click(function () {
            debugger;
            if ($(this).val() == 'Yes') {
                document.getElementById('<%= hdn.ClientID %>').value = "1";
                if (document.getElementById('<%= txtTest.ClientID%>').value == '') {
                    Page_IsValid = false;
                    document.getElementById('<%= hdn.ClientID %>').value = "1";
                }
                else {
                    Page_IsValid = true;
                }
            }
            else {
                document.getElementById('<%= hdn.ClientID %>').value = "0";
                Page_IsValid = true;
            }
            return true;
        });
    });
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div>
        <asp:RadioButtonList ID="rblstTest" RepeatDirection="Horizontal" RepeatLayout="Flow"
            runat="server">
            <asp:ListItem>Yes</asp:ListItem>
            <asp:ListItem>No</asp:ListItem>
        </asp:RadioButtonList>
        <asp:TextBox ID="txtTest" runat="server" />
        <asp:CustomValidator ClientIDMode="Static" ID="cust" OnServerValidate="testSeverValidation"
            ClientValidationFunction="testClientValidation" Display="Dynamic" ErrorMessage="Error!"
            runat="server" />
        <asp:LinkButton runat="server" ID="lnkSubmit" Text="Submit" />
        <asp:HiddenField ID="hdn" runat="server" Value="0" />
    </div>
</asp:Content>

enter image description hereenter image description here

0

All Articles