CustomValidator Example in ASP.NET using C#


Posted in : ASP.Net Posted on : November 30, 2010 at 6:16 PM Comments : [ 0 ]
Advertisements
In this article we will introduce with CustomValidator control in ASP.NET using C#.

CustomValidator Example in ASP.NET using C#

Sometimes we make the user defined validation which depends on the requirement these validation are called
Custom validation. We make a function according to the requirement and the function name pass in the ClientValidationFunction. You can set the ErrorMessage property which you want to display. The another property is ControlToValidate in which you can set the attached control instance. In this example we make a JSValidate function in javaScript. In which you can check the length of character.

CustomValidator.aspx (Design Page):

CustomValidator.aspx (source code):

<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" 
AutoEventWireup="true" CodeFile="CustomValidator.aspx.cs" 
Inherits="CustomValidator" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script language="javascript" type="text/javascript">
function JSValidate(source, args) {
var element = document.getElementById('<%=usernameTextBox.ClientID %>');
if (element.value.length >= 6 && element.value.length <=15)
{
args.IsValid = true;
}
else {
args.IsValid = false;
}
}
</script>
<style type="text/css">
.style2
{
font-size: small;
font-family: Verdana;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div>

Output:

Download source code
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Tutorial Topics