Introduction:

  • In this article we will learn how to use Customvalidator in asp.net or what is Customvalidator in asp.net or why Customvalidator in asp.net with example.
  • Description:

The CustomValidator control allows you to create a validation control with customized validation logic for both the client side and the server side validation.

Let’s see a small example of Customvalidator:

customvalidator.aspx:

 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="customvalidator.aspx.cs"
 
Inherits="AspDotNet.customvalidator" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
   <script type="text/javascript">
       function validateLength(sender, args) {          
           if (args.Value.length < 6)
               return args.IsValid = false;
           else
               return args.IsValid = true;
       }
   </script>
</head>
<body>
   <form id="form1" runat="server">
    <div>
        Password (Client Side):
    <asp:TextBox runat="server" id="txtPassword"  TextMode="Password" />
  <br />
  <asp:customvalidator clientvalidationfunction="validateLength" forecolor="Red"
    
  errormessage="Password cannot be less than 6 characters."
     
controltovalidate="txtPassword"    runat="server" >
               </asp:customvalidator>
<br />
<br />
         Password (Server Side):
 <asp:TextBox runat="server" id="txtpwd"  TextMode="Password"/>
 
<br />
  <asp:customvalidator  forecolor="Red"
     
errormessage="Password cannot be less than 6 characters." controltovalidate="txtpwd"
runat="server" OnServerValidate="Password_Validate" Display="Dynamic">
              
</asp:customvalidator>
       
        <br />
        <br />
                 <asp:button text="Validate" id="btnvalidate" runat="server">
             </asp:button>
    </div>
      
    </form>
</body>
</html>

 

 

customvalidator.aspx.cs

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace AspDotNet
{
    public partial class customvalidator : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            }
        }
        protected void Password_Validate(object source, ServerValidateEventArgs e)
        {
            if (e.Value.Length <6)
                e.IsValid = false;
            else
                e.IsValid = true;
        }
    }
}

 

 

Output:




I hope this article will help to you.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

Welcome to SQL Tutorials Point

Don't have an account? Click here to Create a new account, it takes less than a minute.