In this video learn how to use the validation controls available in ASP.NET to implement validation on your web pages. All the major validation controls such as RequiredFieldValidator and RegularExpressionValidator will be discussed. In addition, learn how to use the CustomValidator when one of the standard validators does not meet your need. Both server and client side validation options are shown. See how to do single validation on a UI control as well as multiple validations.
The following text is a software generated transcript of the video. Click on a minute link to jump to a location within the video
Minute 0
Why this is for spells in the city over to learn how to do validation in ASP net lets get started by building a new website is called its website DB now I ve created some sample page layout for us do validation on ahead of time sputnik get dad had a copy in paste that into our page and as you will see this is just the layout of the page it s nothing to do with the validation is what we have on the page are some typical user input controls and sell it at validation to them they get sorted list of the validation controls are available in ASP net and tears several standard controls get a required field delegator arrange regular expression compare custom
Minute 1
And a validation summary will go and teach those in more detail as the put them on the page and implement plus get started doing some validation personalities controller with an edit page is called the validation summary put that down here below the login button in this control doesn t actually do any validation for what it does provide it is a common place where you can display error messages from the other validation controls that are located on your page in the open up the properties window on the right side because we ll be setting a number of properties on these controls over to do with our validation summary is good and use this as a place where we can provide a list to the user of all the fields on the page and have some sort of air that they need to go back and correct
Minute 2
Type in a standard header here that ll be displayed with a summary control is that you must enter a valid value in the following fields and what can he give this a CSS class I made up a small style sheet inside of the page just to rub it in example of how you might use a style to make your messages sent out a little bit and that s called validation message with the properties of the validation summary control that we can leave in its default setting in which is bulleted list is called the display mode with the display code does is allows you to format the multiple messages in your summary in different formats ballots had our first validation control to the pay
Minute 3
In the first one will add is called the required field validator and this goes as the name implies it requires that the user enter a value in the field take this and let s require that the user enter a last name now we have said several properties in order to have his validate the last name text box on her page first but I would like to do is just putting the CSS classic and validation message and then it for important properties which control is the required field validator associated with and in this case within a select the last name next will we need to do is determine what we want for an error message and this is an important distinction here between error message and the text property
Minute 4
The error message property is the tax that s displayed down here in the validation summary control where is the text is displayed immediately where is the validator is located on the page if the validation fails us were going to do in the air message is put last name because remember in our summary control we have a bulleted list of all the fields that have something invalid so one last name is not entered over going to get down here in the list his last name to the user as an indication after coup appear to last name and in this case enters an information in them down there for text will work in a use is just a capital are so there s a visual indicator to the right of the last name text box to the user if validation fails to
Minute 5
Need to do something on last name is this only if you do to get a required field validator to work on the papal blessing for me to start page and we ll take a quick look at this and actions to get perspective on how it worked for we do the rest of the validator s fury of the page Hyatt will leave less than blank or click login you see we did both the less famously with our validation summary at the bottom as well as the text value in this case capital are indicating to the user that they need to do something with last name let me just put Smith and here and will click login again and this time you see validation passes we get neither the validation summary display nor the text for the validator to the right of last day and all is gone and add some additional validation controls to the page to the next thing we do want to validate on our end
Minute 6
Page is postal code will want to do is to make sure that the user has entered five characters that are all digits nor do this were to be used the regular expression validator and is a very flexible powerful validator that I found useful quite a number of places take regular expression control held it out to the right of postal code like I required validator the first thing we need to do is indicate what control we want to validate the regular expression validator and wanted to postal code and put our validation message class years we get to look that we want next one down to the error message and remember this displays down a validation summary so we ll put postal code then hard to have
Minute 7
To display to the right of postal code text box is postal code must be five numeric digits next we enter our validation expression and this is the regular expression which one evaluated to validate the contents of control him to approach this one is there several predefined regular expressions that you can use or you can type in a custom regular expression and we happen to have one for the US ZIP code so we ll use that celery to test the postal code validation startup page and click login and we get are required for less name but real expression is not a required validator is just checking the contents of a week to
Minute 8
A little getting eras of them typed anything in postal code Souljah states Smith and our last name and let s could ABC and this should break the validation or make it invalid by that and indeed it does get the message that the postal code must be five numeric digits we also see that the postal code is listed in the validation summary as one of the fields that need to be corrected so now I ll just put 12345 log in again passes validation as we don t get either our messages now let s do some validation on the phone number field and what we want to do for phone number is actually perform to validation is one we wanted to be required in its second we want phone number to be in the correct format
Minute 9
Validator survey shows you that you can actually perform multiple validations on a single control on the page coder required field validator code validate and phone number class validation message our message is phone is the invalid field in Texas we want to use in its capital are in for a regular expression control to validate is also the own setup or formatting and message we want displayed in the summary is that phone field techs we want to display out to the right is fallen must be in a format
Minute 10
Area code space prefix or suffix and now for the validation expression will see there is a US phone number but what we re going to do exactly have a piece in a custom validation that I made up ahead of time for our phone number is little bit different than the standard US phone number that we have here in the selection and also shows that he can put in your own custom regular expression so that looks good in one of the things we also have to do here is to count for spacing between the law are and this message from lesbian format because we could have text in here and to NPR would not be displayed but it could be invalid so it s still get the second message and we don t want things jumping around
Minute 11
On the page and we do want to phone message to the way out to the right so what you can do under acquired his Tuesday display property and it has two values which are static and dynamic and non we have a currently static which means that we are or the text of our required field validator always takes up space on the page even when it s not visible over to do is change that dynamic and now we ll watch is that the text of our required validator will only take up space on the page when it s displayed so that will cause the second message from our regular expression off to the right of it to be moved or shifted to the left when the car is not displayed
Minute 12
Indicator for the phone call is less senseless for one last named Smith now let s only could three digits for the phone which should cause the regular expression validator to fail in which it does seize phone still listed at the bottom is having an invalid entry get our message here on what the proper format is so let s put that in and now say this login or submit and both messages are gone because we fully pass validation for both required and rail or expression aspects of the phone number field are let s move on to data birth date of birth we want to three different validations so forceful put a required field validator and after that work for regular expression and the third one is going to be the
Minute 13
In validator to acquire skillful business with them before us to set that up or gave birth contents of the her name is date of birth for our validation message in the box the bottom and the text will be PR and we will make this dynamic salutes Bush is still left it it is valid as a regular expression validator to set up some of the things that were familiar with shoes gave birth validation message class your message will again be date of birth
Minute 14
And then draw a show that on the summary box and attacks will be lead us be in the format of the month day day and for two of four digit year then far of validation regular expression again when they do a custom one of us can paste in a big validation regular expression to have on hand so a false lead with the Baylor expression in the third one is the range validator which we have a look back at and the first two properties are common to the other ones which control or want to validate and class in a message in a field day with his data for
Minute 15
In the and display them over going to say is that they must be between a star with 1850 hopefully no one older than that and today and I ll show you how we get today momentarily the next that we want to set are these minimum and maximum values for the range validation and personal insight is the minimum and it goes to format your Monday salute to 1850 1 1 and then the maximum value which we want to be today delivered in a dissent that can go into the code behind an override download event for the page
Minute 16
Magoo to the arranged validator or one concurrency and maximum value of innocent is equal to the static date Time object today to two string him up and specify that it be here a month today so this will set the maximum value every time the form is loaded to the current date and label them a property that we need to set and this is the type of range that were evaluating its string by default and we wanted to pay to do the date validation and the Kuwait when the patient take a look I remember there s one more properly need to set on the regular expression need to make that a dynamic since we have three in a row here we want that to collapse to the left
Minute 17
Well you know take a look in the page will enter less then no one sat in the phone number and date of birth or to see that blank beginner required validator and that s putting it invalid date formats or just go 33 and we get to message is one that is in the wrong format and also that it s out of range was so valid for men but one that s beyond today a subject message has begun today go back and put it at 1849 still get the message let s change that 19
Minute 18
49 which would be a valid date and indeed it passes validation sets an example of using multiple validation operators in fact three in a row each of the different type to get the result that we want to make this to e mail will do to validations on e mail and the first one that will do will be a required that down and will also do a regular expression acquired a house at the normal properties fetch familiar with now contact class in the field nor the message name in a field is e mail and the text that we want as our Are fantasy which is that the regular expression so
Minute 19
Alumni make that correction and goes to the e mail felonies in this age group could e mail and we will put the capital are some that looks good now go back to the regular expression and all is not lost here because we do want these two properties to be what we ve entered an era message is correct text you want to is not a ballot to e mail format must be e mail at post time for me to see these films validation wrong I notice that the red was not bold
Minute 20
Is good and let s make sure we need to set the dynamic for the display type donated a regular expression and there is one for Internet e mail address so take that it lets the start e mail click login to be to get that it needs to be required to put an invalid e mail now we get the message not a valid e mail is also listed in our validation summary must enter a valid e mail and we ll put Jim that 10 4 and now the mail passes for validation the last two were good you are the password and reenter your password both of these will have a required so that the required validator on both to set the properties tour to validate for the
Minute 21
First one is the password in Qatar formatting our message is in the password to ease the wrong deal and here we are again went down on this one that s okay and will just change this to be the re and her password hint of the text is capital are will go up to the password and will control to validate his password class message is password text is the capital are just go back and check my thoughts
Minute 22
Want this to the password reenter a hand let s just said both these the dynamic for the second validation we wanted when the password will be a regular expression will take the regular expression that on our password lengths set those properties password class K look message for the validation summary listing the fields is password and then the text is the password must include at least one of the following
Minute 23
Viewpoint exclamation had found a dollar percent this is our plus and last but not least and Colin validation expression and were going to use a custom page that in early on I will check for a set sequence of characters within our password then we ll may reenter the password we want to use was coldly compare validator who s who wanted to passwords to be the same sort here will set to different properties won the control to compare and won the control to validate it in a controlled compare it is password cult of validate is he password reenter
Minute 24
Validation message in our air message and news re enter password list field that has a validation era in the text box display is passwords do not match please reenter Alex test out our password validation or approaches click login is to get the two required for password and reenter the password is putting in Haiti regular character password without formal special characters that are required with us but ABC to log in and says it must include monies characters smell of a valid passport will just type password
Minute 25
And will put a at the end of password and then we ll go down and we ll just type password without click login as he would getting that at the validation or a pessimist to not match lets go put and the end of the reenter click login and now the password validation is complete and the last television told him to look at his the custom validator and as the name implies this is a free form validation tool where you have to supply all the validation necessary as let s put this down on password and the need to change the display dynamic because fully decided to do on password is we want to make sure that the user enters a password that said least eight characters long and the control to validate
Minute 26
Use a password validation message in your message is password that is listed in the summary box the bottom head in the text displayed to the right of the password is password must be aged characters or greater now in a trainer validation logic and the first thing we do is write some server side validation logic and double click on that television it adds an event handler to barcode and you can see inbound there are two arguments won this source is the control that s causing the validation on a good knowledge patrol and the second is the arguments which is a value that an Internet control
Minute 27
So we want to do is use the Argus Henderson is valid property and if he said it is valid to true then your validation passes USA the password is valid if you are the value in the arguments is greater than or equal to be an afternoon on the length of back to the page and the other thing what can it do is some client side validation and can see here is a property for the client validation function we need to add that tour page and we will put that right up your header so will a script tag and language equal to
Minute 28
Script and we want to write a function called his length valid and we wanted to have tuned down arguments the source and the arts and then will say Argus is valid is equal to whether or not they are its value length greater than equal to eight and will take that method named and we will go back in to validation cooler to the client function
Minute 29
In and in the server and the reason for that is because if for some reason the client altered the client side JavaScript so that they connect entering a character password it s still then go back to the server if that fails into our standard server side validation cocaine as I set out separate page login passwords required let s type TASS and so we have our special character but the total length of the password is less than a login and now get the client side validation lets get rid of the try login again this time it posted back and we got both the password needs that the special character and the length is equal to 80 smell a center the rest of the valid password and you can see our pass for now has passed all three of its validation
Minute 30
Of us may want to point out is that in any validation control you have a property called to enable client script set to true by default and what this is is that if the browser supports JavaScript then the validation that s been specified is done on the client by default since summary we looked at the different validation controls that come with ASP net and then we looked at how to use them in different combinations some of them are used alone such as with last name we just had a required field validator resume date of birth we had three different validation controls required field regular expression and the range validator we also looked at how to do custom validation pesticide out here with the password length validation that also had any client side validation occurred you to download the samples
Minute 31
Was code that comes with this video take located more detail this Chris Pelz thanks for watching this video on validation in ASP net
Click here to go to original video page
Sunday, August 31, 2008
Video: How Do I: Use Validation Controls in ASP.NET?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment