Here i have explained how to validate input fields through client side and the server side using CustomValidator control in Asp.Net.

Client side validation
CustomValidator control's property "clientvalidationfunction" is used to validate client side validation.

JavaScript

function validateLength(sender, args) {
        debugger;
        if (args.Value.length < 5)
            return args.IsValid = false;
        else
            return args.IsValid = true;
    }

HTML Markup

<asp:TextBox ID="TextBox1" placeholder="Password" runat="server"></asp:TextBox>

<asp:customvalidator ID="Customvalidator2" clientvalidationfunction="validateLength" forecolor="Red" errormessage="Password can not be less than 5 characters." controltovalidate="TextBox1" runat="server" Display="Dynamic">
               </asp:customvalidator>

Server side validation
CustomValidator control's property "onservervalidate" is used to validate server side validation.

C#

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (args.Value.Length < 5)
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }

HTML Markup

<asp:textbox placeholder="Password" runat="server" id="TextBox2"> </asp:textbox>

<asp:customvalidator id="CustomValidator1" forecolor="Red" onservervalidate="CustomValidator1_ServerValidate" errormessage="Password cannot be less than 5 characters." controltovalidate="TextBox2" runat="server" Display="Dynamic">
             </asp:customvalidator>

<asp:button text="Submit" id="Button2" runat="server">  </asp:button>

Screenshot

Add Comments