Client Side CustomValidator Example using Javascript in Asp.Net C# VB.Net

Sometimes existing Asp.Net validation controls does not meet our requirements. In that case we can define custom validation function and call it using the CustomValidator control to check user input before the page is submitted by writing a Javascript function. To use the CustomValidator control we need to know some important properties of this control like:

ControlToValidate: Need to define the input Control here like TextBox.
ErrorMessage: The message you want to show whenever validation fail.
ClientValidationFunction: This is the key property to validate a control from client side using Javascript. Write a Javascript validation function over the targeted control and assign the function name here.
ValidationGroup: Need to provide a name if your page has different group of validations for different controls.

Related Article:
Server Side CustomValidator Example in Asp.Net C# VB.Net

To do the Example use the below HTML markup:

Password: <br />
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Length must be >6 !!" ControlToValidate="txtPassword" ClientValidationFunction="validateLength" Font-Italic="true" Font-Bold="true" ForeColor="Red" ValidationGroup="Registration"></asp:CustomValidator><br />
Confirm Password:<br />
<asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password"></asp:TextBox><br /><br />
<asp:Button ID="Button1" runat="server" Text="Validate" ValidationGroup="Registration"/>

Javascript function to Validate a TextBox Control using CustomValidator:

    <script type="text/javascript">
        function validateLength(sender, args) {
            args.IsValid = (args.Value.length > 6);

Client Side CustomValidator Example using Javascript in Asp.Net Csharp VB.Net

Hope now you can use CustomValidator from client side using Javascript function.

Download Code Example C#        Download Code Example VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code