In my previous article I explained about predefined validation controls in Asp.Net. In this article we discuss about how to create our own validation control (i.e., custom validation control) with our own functionality.
BaseValidator is the base class for each and every validation control in Asp.Net (including predefined validation controls RequiredFieldValidator, RegularExpressionValidator,…..etc). BaseValidator is the abstract class (i.e., every validation control has to inherit BaseValidator class), which has abstract method EvaluateIsValid().
If you want to create our own custom validation control, you have to inherit BaseValidator class and have to implement EvaluateIsValid() method which return boolean value based on the input. BaseValidator class several other normal methods including GetControlValidationValue() which retrieves the value of the control being validated.
To create custom validation control in Asp.Net, you have to override EvaluateIsValid() method of BaseValidator class and within the EvaluateIsValid() method, call GetControlValidationValue() to get the value of the form field being validated.
Here we create the integer validation custom validation control in Asp.Net which gives the error if user eneters the value other than integer as shown below.
namespace CustomValidators
{
///<summary>
/// Summary description for IntValidation
///</summary>
public class IntValidation : BaseValidator
{
public IntValidation()
{
//
// TODO: Add constructor logic here
//
}
protected override bool EvaluateIsValid()
{
String value = this.GetControlValidationValue(this.ControlToValidate);
return IsNumeric(value.Trim());
}
public static bool IsNumeric(string information)
{
if (Regex.IsMatch(information, "^[1-9][0-9]*$") == true)
return true;
else
return false;
}
}
}
As shown above, we create the IntValidation class which inherits the BaseValidator class and overrides the EvaluateIsValid() method. It uses the GetControlValidationValue() method to pass the control value to EvaluateIsValid() method.
We have to declare this control in Asp.Net page as shown below.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="CV" Namespace="CustomValidators" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Custom Validation Control in Asp.Net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label id="lblAcctNum" Text="Enter Your Account Number:" AssociatedControlID="txtAcctNum" Runat="server" /><br />
<asp:TextBox id="txtAcctNum" Runat="server" />
<CV:IntValidation id="intValue" ControlToValidate="txtAcctNum"
Text="Account Number Must be Numeric" Runat="server" /><br /><br />
<asp:Button id="btnSubmit" Text="Submit" Runat="server" />
</div>
</form>
</body>
</html>
Declare the custom validation control in asp.net page by using Register directive as shown above. If you want to use this control for multiple pages, register this control under <pages> section in web.config file of your application.