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!
Instead of checking for every possible value that users may enter again and again in every page you make, you better use the ASP.Net validation controls which we will be talking about in detail in this tutorial. What I love about the ASP.Net validation controls is that they run on both client side and server side, client side rely on the JavaScript settings of the client's browser, this will save users round trips to the server for validation but what if JavaScript is disabled on the browser? The validation will occur on the server side and you don't have to worry about that or write extra code, great!
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.
If you disable the JavaScript for your browser, the same thing will happen except it now happens on the server side, you will notice that the page is posted back.
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!
asdfasdf
how to stop page postback when the validation using ?
Explained in better way..thanks I learned..
Thanks for the information. That was helpful
- How to Adjust Audio/Video Sync In An AVI File Using VirtualDub
Rating: 3.7/5 Votes: 172 - Fit Text in a Shape
Rating: 3.6/5 Votes: 112 - Dynamic Mask in Actionscript 3
Rating: 3.5/5 Votes: 11 - Limit Characters From Your Text
Rating: 3.5/5 Votes: 15 - Show the Url of the Page
Rating: 3.5/5 Votes: 34








Thnx.......