If you are an ASP.Net developer you must have every now and then felt the need of a custom validation control. I felt the need. May be that is the reason I am writing this simple tutorial to create a custom length validator control.

In order to create this control we will be using the BaseValidator class which as the name suggests is the base for all the validation controls that is provided by default in visual studio and ASP.NET. BaseValidator is an abstract class i.e when you are deriving a new control from this class you will have to implement a method of that class.

One important method that we would have to override is the EvaluateIsValid() method. This method returns true only if the form control that is being validated is valid.

There is one very important and most useful method that we will be using in the development of this custom validator control. GetControlValidationValue() will be used to retrieve the value of the control that you will be trying to validate. We will be using this function in the overridden EvaluateIsValid() method.

We will create a a class file named CheckLengthValidator.cs. You will be asked to place this file directly in the App_Code folder of the website. All the files in this folder is automatically compiled during initializations.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

///
/// Summary description for CheckLengthValidator
///
///
namespace mycontrol
{

    public class CheckLengthValidator : BaseValidator
    {
        public CheckLengthValidator()
        { }

        int _maxLength = 0;
        public int MaxLength
        {
            get { return _maxLength; }
            set { _maxLength = value; }
        }

        protected override bool EvaluateIsValid()
        {
            string value = this.GetControlValidationValue(this.ControlToValidate);
            if (value.Length > _maxLength)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

    }

}

Now, in order to use this control that we just created, we will have to add a Web form. You can name it anything that you like.
And add this code in it.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ValidationItems.aspx.cs"
    Inherits="ValidationItems" %>
<%@ Register TagPrefix="custom" Namespace="mycontrol"  %>

<form id="form1">
<div>
<fieldset>
<legend>Item Details </legend>

Item Name:

            Price:

            Quantity:</fieldset>
</div>
</form>

In the code behind file add the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using mycontrol;

public partial class ValidationItems : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Page_Prerender(object sender, EventArgs e)
    {
        foreach (BaseValidator valControls in Page.Validators)
        {
            WebControl controlName = (WebControl)Page.FindControl(valControls.ControlToValidate);
            if (valControls.IsValid)
            {
                controlName.BackColor = Color.Yellow;
            }
            else
            {
                controlName.BackColor = Color.Green;
            }
        }
    }
}

You are good to go. Feel free to comment. 🙂