ASP.Net Validation Controls Part 1
Do you remember the ASP.Net If Statement tutorial? What if your user didn't enter a value or try to enter a non numeric value? You will get an error: "Input string was not in a correct format" because the int.Parse function will not be able to parse null or non numeric values. The rule is NEVER rely on your users to enter the right values! Some will enter invalid values by mistake while others will do that on purpose trying to hack your site!
Let's open our Visual Studio, drop a TextBox named txtInput, a Button named btnExecute and under the Validation tab of the Visual Studio Toolbox window drop a RequiredFieldValidator Control and move it next to the txtInput so your layout looks like that:
The first important and common property in all the ASP.Net validation controls is the ControlToValidate and you will get an error "The ControlToValidate property of 'RequiredFieldValidator1' cannot be blank" if you try to run your page without setting it, so choose txtInput from the dropdown list of the ControlToValidate property.
Another common property that you will change all the time is the ErrorMessage which specifies what your users will see if the validation control does not validate, and that's it, no single line of code is required!
Run your page, try to click the btnExecute without entering a value in the txtInput and you will see the error message that you specified next to the txtInput, enter any value, press the button and the error message will disappear and the page is submitted.
So the ASP.Net Required Field Validator makes sure that you have entered a value in the TextBox but what if we want to make sure that the value the user has entered is in the correct format? ASP.Net has different validation controls depending on the type of validation you want and we will learn how to use them one by one.
ASP.Net Regular Expression Validator control is very useful for validating user input depending on a Regular Expression. Regular Expressions are not the easiest to write but you can search the internet for the regular expression that you want, there are a lot of sites for that.
Drop a Regular Expression Validator next to the Required Field Validator, set its ControlToValidate property to txtInput and change its ErrorMessage property to "Invalid email format".
Look for the ValidationExpression property where you could write your regular expression or choose from the ready made ones that RegularExpressionValidator has, so choose Internet email address and you will get "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" which is the regular expression for checking the email address format - it's not the easiest to write, right? - and click ok.
Run the form, try clicking the button without entering a value and you will see the Required Filed Validator error message, try typing email in a wrong format and you will get the Regular Expression Validator error message. But why there is a blank area next to the Regular Expression Validator control? It's the place reserved for the Required Field Validator, to fix that, change its Display property from Static - the default - to Dynamic.
In the next part of the ASP.Net Validation Controls tutorial we will learn about more validation controls and more of their important properties. So stay tuned!
- How to Adjust Audio/Video Sync In An AVI File Using VirtualDub
Rating: 3.6/5 Votes: 188
- Show the Url of the Page
Rating: 3.5/5 Votes: 43
- Changing Hair Color Using Photoshop
Rating: 3.4/5 Votes: 71
- Fit Text in a Shape
Rating: 3.4/5 Votes: 128
- Shiny Buttons
Rating: 3.4/5 Votes: 19