Home Page

Click here to download the sample source code for this lesson

This lesson is a continuation of the discussion regarding ASP.NET validation controls.  Validation controls do not require any coding, thus saving time performing redudant tasks.

In this exercise we will cover how to ensure that the user typed information according to a rule expression.  ASP.NET offers the RegularExpressionValidator control to ensure that fields follow a custom rule.  The control comes with some common built-in rule expressions that you can choose from or you can define your own regular expression for the rule.  A couple of the practical built-in expressions are the e-mail address expression and the URL expression.  The RegularExpressionValidator is very easy to use; just drag it from the Toolbox window onto your web form.  It is located in the Validation group in the Toolbox window, so you may have to scroll down the Toolbox to find it.  You should drag the control to the location where you want the error message to show up, in case the validation fails; which in this case is if something entered in a field doesn’t match the expression rule.

Once you have dragged the validator onto the web form, you need to set some of the properties.  The ErrorMessage property sets what error text will be displayed in case the validation fails.  The error text will be shown in red.  The ControlToValidate property sets which server control to examine at runtime to see if it follows the rule.  Finally, the ValidationExpression property defines the regular expression rule that will be used.  To choose from the list of built-in expressions click on the … ellipses button.

Click here to watch an example video.  In the example I dragged a RegularExpressionValidator next to the e-mail TextBox control.  Notice that I set the expression for the rule by choosing from the built-in list of expressions.  When I run the program, if I don’t type a valid e-mail address, the validator will display the error message.

4 Comments »

  1. Ted, I wanted to thank you for taking the time to sit down and make tutorials for how to do your amazing examples. Thanks man you rock!

    Comment by joel — July 1, 2009 @ 3:48 pm

  2. hi, i didn’t leave any site to find my question about my validayionexpression in my project,but more searching more finding nothing, none in mdsn.con non in oher sites

    but i found my way in your examples in http://regexlib.com/CheatSheet.aspx page

    our prophet “Mohammad” says: who didn’t thank humans, didn’t thank the god! I THANK YOU

    Comment by Hadi Ahadi — September 30, 2009 @ 1:04 am

  3. i want to validate my text box name so that i should accept only characters in it. can any one guide how to do it.

    Comment by sachin — March 22, 2010 @ 3:23 am

  4. Some time before, I did need to buy a car for my firm but I did not earn enough cash and couldn’t order anything. Thank God my mother proposed to take the home loans from banks. Hence, I acted that and was happy with my consolidation loan.

    Comment by CoraPadilla18 — August 28, 2011 @ 12:04 am

RSS feed for comments on this post. TrackBack URL

Leave a comment