Click here to Skip to main content
13,193,770 members (36,696 online)
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 9 Jan 2014

ASP.NET Validation Made Easy

, 9 Jan 2014
Rate this:
Please Sign up or sign in to vote.
ASP.NET textbox validation made easy.


Every time I used ASP.NET and Ajax validation, I didn't like it. I literally wrote more than 5 lines of XML to validate a single textbox control, it took too much time, made debugging code very difficult and I don't like too much code in one place. Then I got this idea. I made a user control for textbox and wrote validation for it, now if you have 5 or 6 textboxes in one form. You will have to write 5 or 6 lines for all of them.

Using the Code

First, I created a user control for the textbox the code is as under:


<%@ Control Language="C#" AutoEventWireup="true" 

CodeFile="TXBXX.ascx.cs" Inherits="UserControls_TXBXX" %>
<%@ Register Assembly="AjaxControlToolkit" 

Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
<asp:textbox id="TX" runat="server" textmode="SingleLine" />
<asp:requiredfieldvalidator id="RF" runat="server" 

errormessage="-" controltovalidate="TX" 

display="None" setfocusonerror="True" />
<ajax:validatorcalloutextender id="VC"  runat="server" 

targetcontrolid="RF" enabled="True">


public partial class UserControls_TXBXX : System.Web.UI.UserControl
    public string Text
            return TX.Text.Trim();
            TX.Text = value.Trim();

    public string CSS
            TX.Attributes.Add("class", value);

    public string Validation
            TX.ValidationGroup = value;
            RF.ValidationGroup = value;

    public string ErrMsg
            RF.ErrorMessage = value;

    public TextBoxMode TextMode
            TX.TextMode = value;

    protected void Page_Load(object sender, EventArgs e)


And this is how we use it:

Import the control:

<%@ Register Src="../../UserControls/TXBXX.ascx" 

TagName="TXBXX" TagPrefix="TXBX" %>

Use the control:

<TXBX:txbxx runat="server" textmode="MultiLine" 

validation="newnote" errmsg="Please Provide Name" id="TX_Name" /> 

Here is an example of this same code you would use otherwise:

<asp:textbox runat="server" id="TX_Name">
<asp:requiredfieldvalidator setfocusonerror="true" 

errormessage="Please Provide Name" display="None" 

validationgroup="newnote" controltovalidate="TxBx_Name" 

runat="server" id="RFV_Name">
<ajax:validatorcalloutextender enabled="True" 

targetcontrolid="REV_Name"  runat="server" id="VCE_REV_Name">

Points of Interest

It helped me in coding quickly, it saved my time, my code is clean and short and I can easily set values of a textbox at one go. I hope it helps you too. :) Please send me your feedback on


  • 9th January, 2014: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Team Leader
Pakistan Pakistan
Working in the field of software development from the past 9 years, love to work in databases and writing applications for large enterprises, experienced in medical and financial sectors.

Believes that software development is the most beautiful work in the world.

You may also be interested in...


Comments and Discussions

GeneralMy Vote of 4 Pin
tanweer9-Jan-14 17:51
membertanweer9-Jan-14 17:51 
GeneralRe: My Vote of 4 Pin
nasir_ml10-Jan-14 17:37
professionalnasir_ml10-Jan-14 17:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171018.2 | Last Updated 9 Jan 2014
Article Copyright 2014 by nasir_ml
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid