cancel
 

 

Job type :

Zip Code :

community forums

Featuring over 100 topics of interest to DoItYourselfers.

How to Use the Regular Expressions in ASP.NET

AlertThis content requires Flash

To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

Download the free Flash Player now!

Get Adobe Flash Player

Video Transcript

Let us look at the email address next, we are going to use a required regular expression validator here to match the particular pattern that you expect in an email address entered in the dots and @ signs and so on.

First things first, let us take a required field validator and copy it and paste it and let us reconfigure it down here as before. So we set the ID to be email required field validator and we set the control to validate, email textbox and we will change the ErrorMessage, email address required. And we will change the text property as well to the same, email address required and that is all done and that means we can now add a required regular expression validator onto our panel. And let us set the properties for that.

So first of all, we set the ID to be email regular expression validator. We will set the control to validate as always to be email textbox. We will set the Display to be dynamic. We will set the ErrorMessage, now at this point, I am not even going to attempt to explain to the user what the email address should be. We assume that they can figure out for themselves, so we just display a more generic ErrorMessage here, invalid email address, and keep on going. Set focus on error and we set that to be true, text message, invalid email address and the crucial thing for a regular expression validator is the validation expression.

Now, if you are familiar with regular expression syntax, you can type it in here directly if you want to or alternatively, if you can click the ellipses, there are some provided already by the dot net framework to help you out. So for example, we have a French phone number and here is the one we want, internet email address. So this is kind of complex regular expression. I am going to just quickly explain what it means.

First of all, the /w, / is an escape character and /w represents a word character such as a letter, and the plus character is the multiplicity indicator and that means we can have one or more word characters like the letter A at the start of the email address. The next part of it enclosed in parenthesis and followed by the asterisk. The asterisk is also a multiplicity indicator and it means zero or more of everything enclose inside the parenthesis.

Now, the square brackets provide a set of alternative characters, so it means you can have a hyphen, or a plus symbol, or a dot, or an apostrophe. You can have any of those characters followed by one or more word characters and that whole pattern, enclosed in parenthesis can be repeated zero or more times because of the star. So that is the first part of the user's email address.

And then we have the @ symbol, so let us go up here literally in the email address. And then after that, we have one or more word characters followed by inside the parenthesis and followed by the star. We have zero or more occurrences of a hyphen or a dot followed by one or more word characters. Now, the backslash dot means you must have a dot character in this part of the email address after the @ symbol, you must have at least one dot there, followed by one or more word characters, followed by zero or more hyphens or dots or word characters, okay. So for example, andy.olsen@totaltraining.com would be good. So that is how a validation expression.

Now, if you want to find out more about regular expression syntax, there is quite a lot of help available on the MSTN help system. So on the help menu, click on Index and in the Index, I type regular expressions and the most useful item I found is about regular expressions here. So it gives you various stuff in that point for you to find out more about how regular expressions work.

Okay, so feel free, this is quite a deep area, so you probably want to spend quite a while just to immerse in yourself in the theory here. The examples to be honest, I found the examples not us usual as they might be they tend to be a little bit cryptic. You will probably want to work through these sections and the order specified here to get a better understanding. So we will just skip over that for now and we will stick w
Let us look at the email address next, we are going to use a required regular expression validator here to match the particular pattern that you expect in an email address entered in the dots and @ signs and so on. First things first, let us take a required field validator... click to read more


How to Build a Shoe Storage Bench

sponsored articles of the day

diy centers

Research and explore a wealth of wisdom on these topics