TutorialsRoom.com
RSS Feed

Subscribe: RSS or e-mail

Home » Web Development » ASP.NET »

ASP.Net Validation Controls Part 1

ASP.Net Validation Controls Part 1
In part one of this tutorial, we will learn about the ASP.Net controls that will make your life much easier and make your site and applications much more professional, ASP.Net Validation Controls.

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:

Asp.Net Validation Controls

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.

Asp.Net Validation Controls

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.

Asp.Net Validation Controls

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!

Rating: 2.5/5 (63 Votes)
5 Comments
bhagirath said,
December 6th, 2011

Thnx.......

asdfasd said,
August 6th, 2011

asdfasdf

Rathod Azaj khan said,
May 11th, 2011

how to stop page postback when the validation using ?

shivu said,
April 26th, 2011

Explained in better way..thanks I learned..

Shweta said,
September 2nd, 2009

Thanks for the information. That was helpful

Post Your Comment (In English):



(Optional, will not be shown)


(Optional)




Please notice that comments which are not related and those with live links will be deleted.

Copyright © 2007-2013 Hazem Osman. All rights reserved. Terms & Conditions