locked
Duplicated RRS feed

Answers

  • User214976711 posted

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx

    I am employing client-side validation. From the example above, we found that they use both validation on client-side and server side functions. Can we remove OnServerValidate from CustomValidator?

    Yes can remove OnServerValidate, here is the simple example, which demostrates, if the user enters more than 6 characters

    ClientValidationFunction="ValidateUserName"

    is fired and returns "args.IsValid = false":

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type="text/javascript" src="jquery-1.7.1.js"></script>
        <script type="text/javascript">
    
            function ValidateUserName(source, args) {
                var textBox = $('#<%= TextUserName.ClientID%>').val();
                if (textBox.length > 6) {
                    args.IsValid = false;
                    return false;
                }
                args.IsValid = true;
                return true;
            }
    </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextUserName" runat="server"></asp:TextBox>
            <asp:CustomValidator ID="CustomValidatorUser" runat="server" ControlToValidate="TextUserName"
                        ErrorMessage="Minimum of 6 (six) alphanumeric characters." ClientValidationFunction="ValidateUserName"
                        Display="Dynamic" ValidateEmptyText="True"></asp:CustomValidator>
        </div>
        </form>
    </body>
    </html>



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 23, 2012 10:08 PM