Geekpedia Programming Tutorials






Performing custom validation

Demonstrates how to use custom validation on a form by checking to see if a string in a TextBox is not the same with a string from an array (or database), case in which it displays 'This name is already taken'.

On Wednesday, August 11th 2004 at 04:57 AM
By Andrew Pociu (View Profile)
*****   (Rated 4.3 with 15 votes)
Contextual Ads
More ASP.NET Resources
Advertisement
You'll often want to use custom validation to validate a control on a webform. Perhaps when a user registers on your website you want to check the database to see if the name he wants to register doesn't already exist. If you don't know the basics of form validation, first read the tutorial named 'Validate using RequiredFieldValidator'.



Start a new ASP .NET Web Application project named customValidation and drag a TextBox, a Button and a CustomValidator to WebForm1.aspx.

Now set the ErrorMessage property of CustomValidator1 to "This name is already taken". Also set the ControlToValidate property to TextBox1.



Now let's get into the C# code. We won't bother with a real database in this project, we will replace it with an array that holds 3 names. So in the public class WebForm1 make a string array:





string[] names = {"Van Nostrand", "Vandelay", "Pennypacker"};



Now we'll use the following code to loop through the array and check to see if the name inside the TextBox matches one of the values in the array:





public void validateName(object sender, ServerValidateEventArgs e)

{

   foreach(string name in names)

   {

      if(e.Value == name)

      {

         e.IsValid = false;

      }

   }

}



At this time we only need to set the ServerValidate event to validateName. We'll do this using the Properties window of Visual Studio .NET:





...and that's all! Open the webpage and see for yourself - enter one of the names: 'Van Nostrand', 'Vandelay' or 'Pennypacker' and you'll get the error:











NOTE

It won't take you much time to realize that this validation is case-sensitive. If you enter vandelay it will validate with no problems. You'll probably want to change this to case-insensitive. To do that just convert the names from the TextBox in all uppercase or lowercase (using e.Value.ToUpper()) and the text from the database (in this case, arrays) also.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by George on Monday, November 29th 2004 at 09:33 AM

I have no ServerValidate Property for my customValidator control in VS.Net 2003?

by Adithan on Friday, January 27th 2006 at 07:49 AM

Custom Validation in Asp.Net : Get input only integer with float value

by Pramod sarwar on Thursday, September 14th 2006 at 07:32 AM

It\'s grate solution u have provided but same i want to get by using database will u plz provide me solution or hint because i spent my whole day in that.

by Ujjwal Soni on Tuesday, August 12th 2008 at 01:11 PM

Excellent tutorial. Its really helpful.

Thanks,

Ujjwal B Soni

by sathish on Tuesday, November 25th 2008 at 05:36 AM

thank u
its very nice......


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs ASP.NET Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons