Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Regular Expression Validator - Course ASP.NET 4 and Visual Studio 2010 - Lesson 17 | DevMedia .net courses

In this video, we will talk about the RegularExpressionValidator an ASP.Net validator. In some situations, the data follows a certain format (like a social security number and a telephone number).

Duration:  16 minutes

Summary: In this video, we will talk about the RegularExpressionValidator. In some situations, the data follows a certain format (like a social security number and a telephone number). For these controls we can use the RegularExpressionValidator to validate the data entered by the user. Many of the properties of the RegularExpressionValidator match the validators that we have already discussed. However, the time consuming task is building the validation expression. There is some syntax that you need to get familiar with to build an expression, but Visual Studio already comes with few built-in expressions to simplify things (like phones numbers in different countries and email addresses). There is also a web site that contains a library of already built in regular expressions ( http://regexlib.com). You can search the library to find a match. The match will list the rating, the actual expression, and some good/not good matches. On this site you can also find a cheat sheet that helps you build your own expressions.

Methodology of the development of example: Good coding standard and simplified design to illustrate the key points.

Technology Used: ASP.NET and Visual Studio 2010

Keyword: ASP.NET 4, Visual Studio 2010, RegularExpressionValidator, ValidationExpression, ControlToValidate, Text

Transcript: Welcome back, everybody. This is Ayad Boudiab. I'm a Ssenior .NET application developer, and in this series of videos, we've been discussing ASP.NET. We're covering all the details of ASP.NET, especially for ASP.NET 4. But as you know, there are a lot of basic concepts that we need to cover until we get to that point.

In the last three or four videos, I've been discussing validation. So we have different ways to validate our data. We have the compare validator. We also talked about the range validator and the required field validator. There are different ways to validate our data, and we need to make sure that this validation happens on the client and on the server side to be able to catch any issues before we save our data to a storage location.

Now, in today's video, I need to concentrate on regular expressions. So when we talk about regular expressions, we talk about a certain pattern of data that would make sense to us. For example, when I'm filling out a form, I might ask the user for an email address or a phone number. You know that email addresses follow a certain pattern. For example, something dot something at Gmail.com, right? So we have the @ sign, and then we have the Gmail.com. We know that we are following a certain pattern there. For the phone number, at least in the States, we have the area code first, then the seven-digit number, maybe a dash in between. So we are following a certain pattern, and we are asking the user to enter the data in that specific pattern. If this pattern is not satisfied, then I will get the error message, and, of course, my data would not be submitted.

In this example, we will try to look at a couple of things. We will start with asking the user to enter his email address, and we're trying to see how we can validate that email address based on certain criteria. So let's go ahead and scroll down here. I'm still using the same page, by the way, which is userdata.aspx, and I need to ask the user for the email address, right? So I'm going to drop our usual label in a text box. Label here. Underneath the label, we've got the text box. Underneath the text box, I'm going to put my validator, which in this case is a regular expression validator. And then I'll put the break.

For the label, I need to ask the user for the email address, so LBL email, text, "Please enter the email address". And this one is a TXT email. Here goes my regular expression validator. We have an error message and the close. As we always do with a regular expression validator, or any other validator, we have an ID. After all, it's a control, so it has its own unique ID. And by the way, I can use that ID in the code behind to manipulate my validators.

Now, as I mentioned to you in the previous videos, the way to connect the validator with the control that you are interested in is through the control to validate property. You don't have to drop the validator underneath the control you're validating. The validator could be anywhere on the page, and the way to connect the validator with the control you're validating is with the following property, which is the control to validate. So in this case, my control to validate is TXT email. So I know I'm validating the email address that the user has typed.

Now, the text, which is the short description that tells the user what's wrong if they did not enter the right data, is something like, "Invalid email," right? That's enough to say what's going on. And the error message, as you will see later in the validation summary, will give more details to the user.

So far so good, then. I have my control to validate and the text property, and of course it's on [inaudible 5:08] server, and I have an ID. Now the question is, how would I know that this is a correct email address? I must have some validation expression that tells me how to validate that. That's the key point when we work with regular expression validators, because we have to build that validation expression, and building the validation expression is not an easy task because it has some characters that you need to put together to explicitly specify how the format of the email address would be.

Now, ASP.NET already gives us some familiar concepts, some familiar expressions that I can use, especially when it comes to email and phone numbers, because these are usual expressions that we can handle. So let's look over here. I have the regular expression validator. I need to look at its properties. Let's maximize that a bit. And then for the regular expression validator, I need to scroll down in here and see what I have for the list of properties that I can work with. I have, of course, the text, which I modified. You notice also I have the error message, which is over here, regular expression validator. And I also have the control to validate, which is the TXT email. If I scroll all the way down, I will see the validation expression. This is the expression that I can use to validate my data. So this is where I can type my validation expression to be able to determine whether that data is valid or not. So then the expression that I'm interested in is for the email address, right?

Now, having saved my code, I can click on the validation expression. It says, "Regular expression to determine validity," right? I have in here a way to go to a certain dialog, a small button with "...", right? So I click on it, and I have regular expression editor. The regular expression editor has a list of expressions that are already built for me. I can choose any of those to be able to use them within my code, like French phone number, French postal code, German phone number. You notice the regular expression that's given to me down here. I can copy this, take it to my code, and I can also manipulate it, right? So in here I have internet email address. I can select this one, and it gives me the email address, and I say, "Okay, that's the one that I'm interested in." You notice if I show some space here, you notice the way the expression is for building an email address.

Now, this will be able to tell me that I have some type of text, and then I have an @ sign, and then some other type of text, right? So this is what the email address would look like. Let's go ahead and save, and then right click on the page and view in browser. The page will load shortly, and I will have the "Please enter the email address". Let me type some text, and I get out of it, and it says "Invalid email". How about something like this? AB@Gmail.com. Tab out. So it sounds like that this matches the expression that I'm interested in. So that way I will know that this is a valid email address.

Of course, the hardest part of actually working with the regular expression is actually building one, because when I already selected an available one, it took the validation expression, and it put it over here. So I already have my validation expression. The question is, how about when we need to build our own? There is a little bit easier way to do this than just typing it yourself. If you go to the Internet and you look at the following website, regexlive.com, it's a regular expression library that has many regular expressions already built in. I can look at this website. I can search for specific expressions, and then use them within my code.

Let's look at small examples here. By the way, before I type in my example, notice on the left-hand side, it says "Regular expression cheat sheet". Let's click on that. It gives me some ideas of how to build my regular expressions. It says that when you use the caret sign like this, this is the start of a string. So if I use caret ABC, I'm looking for something like ABC, ABCDEF, ABC123, so anything that starts with ABC. The dollar sign is the end of a string. So if I say ABC dollar sign, then this is the end of an expression. For example, ABC, or the following, ABC, or 123ABC. So this is the end of an expression. The dot matches any character. The or in here matches alternation, like Bill or Ted, Ted comma Bill, either one of them. And then I have the curly brackets, explicit quantifier notation. So if I use the curly brackets ABC two C, that means, for example, the B could happen twice. And the same thing for the parentheses. The star matches zero or more previous expressions. The plus is one or more. The question mark is zero or one. And then the backslash, which is preceding one of the above, it makes it a literal instead of a special character.

So, as you can see, there are different ways of building those regular expressions. This website will help you a lot to build one. If you get familiar with these charts and how you can define and build your own patterns, you have to be careful, though and do explicitly a lot of testing to make sure that the data you're trying to validate makes sense, because small mistakes could cost you a lot. You don't want invalid data to be saved in your data storage location. So that's why it's very important to test your expressions very well once you build them.

Now let's go back over here to the main page. I would preferably print out that cheat sheet and tape it on my desk so I can get familiar with all of the characters that I can use. But say, in here, I come back to the main page and I enter a specific keyword. For example, earlier I was looking for the setup for international phone numbers. So I type "International", and here is the text that I typed earlier, international phone number, and I click search. That gives me different options for using the international phone number. Did I type that right? Let's do that again. International phone number and I click search. Maybe I typed enter earlier, and it reloaded the page. At any rate, I have the different expressions for international phone numbers. It gave me the regular expression that I'm interested in, and it gave me a rating on it. See, this one seems to have a good rating. The one below it has a little bit of a lower rating, and the one below it is even a lower rating. So I go to the top and I use the one that I'm interested in. It gives me the expression. It gave me the description, "International phone number check. Optional country code followed by area code and surrounded with a dash or parentheses." And it gave me some good matches, and gave me some non-matches.

So I can take this one and I can use it in my code. I copy that expression, and then I can go to my page. I can drop in here another regular expression validator. Let's stick some space here. [inaudible 14:36] server, with the error message. Save that, and then I can click on it, or I can go F4. It takes me over to the properties, and then I have my validation expression, and I can paste the expression that I am interested in. As you can notice, this expression is already put inside my regular expression validator. I can drop a text box here, and then I can associate that regular expression validator with the text box to be able to validate international phone numbers.

So as you can see, there are multiple ways to help you build your own expressions, and usually the people that post to that site are experts. They already know what they are doing. So I prefer that you go to that site and use the expressions that they have. But of course, if you cannot find what you are looking for, at least find something that you can start with, and then customize it to fit your needs. As you can see, building regular expressions is very interesting and saves you a lot of headache when you are expecting certain text from the user that matches a certain pattern.

I hope this discussion of regular expressions helps you building your own websites and validating the users' data, of course. Please drop us a line on Mr. Bool's website if you have any questions or concerns. Thank you very much for listening. I'm Ayad Boudiab, and you take care.



Senior Software Engineer with experience in C#, JavaScript, jQuery, Knockout JS, Kendo UI, HTML5, and SQL Server. Ayad has more than 20 years experience in technical training and teaching. Contributor to MrBool for about 7 years.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 2,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 1,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,33
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login