Asp .net Validations – simple tutorial by ra00l

Introduction

         In order for you to follow this tutorial, you will need to have .NET Framework , Visual Studio .NET installed on your computer and IIS server installed and configured.

Getting to work

         In this article, we are going to use validation controls provided by ASP .NET in order to force the user to input correct information. You can enable validation of user input by adding validation controls to your form as you would other server controls. There are controls for different types of validation, such as range checking, pattern matching, comparison to a value or required entry. More than that, you can define your own controls for checking user’s entry using validation logic that you write yourself.

         Each validation control references a server input control somewhere on the page. When the user’s input is being processed, ASP.NET passes the user’s entry to the appropriate validation control or controls. The validation controls test the user’s input and set a property to indicate whether the entry passed the test. After all validation controls have been called, a property on the page is set; if any of the controls shows that a validation check failed, the entire page is set to invalid. The property for the page is called IsValid and return a boolean value indicating whether the validation controls on the page were all valid. Also, every validation control has a property with the same name witch indicates whether the user data has passed the validation process or not. When the form loads, the validation controls are hidden. However, if the control detects an error, it produces error message text that you specify. The error message can be displayed in a variety of ways such as: next to the control, at any other place on the page and next to the control a small asterisc or some short text or wherever in the page the programmer sets the costom validation control to display in. ASP .NET performs validation on the server side, when the form is submitted. If a validation control doesn’t passes the validation, the page is set to an invalid state and the programmer must test it for validity before the code runs. The coolest thing about validation controls is that, if the user has a DHTML enabled browser, the validation occurs on the client browser, thus improving the response time of the page.

        Now let’s start building our application. First, you will need to create a new ASP .NET Web Application (select File -> New -> Project -> Visual C# Projects -> ASP .NET Web Application ). Name the project whatever you want ( mine is named ValidatingTextboxes ) and let’s add some controls to the application.

         Let’s add first a Label and a Textbox. Set the Text property of the Label to User Name: and the ID of the Textbox to tb_userName. Next to the Textbox drag and drop a RequiredFieldValidator, set it’s ErrorMessage property to User name cannot be blank! and its ControlToValidate property to the ID of the Textbox added above (here, it’s tb_userName). The ErrorMessage text will be shown next to the Textbox if the user leaves the Textbox empty. Next add a RegularExpressionValidator and drag it over the the RequiredFieldValidator added above. Set the ErrorMessage property to User Name must be at least 7 characters long and the ControlToValidate property to tb_serName. Also, set the ValidationExpression to the following ^[w]{7,}, witch means that the expression validates to true if the user enters any character, but at least 7 characters long.

         Add another Label control and set its Text property to Password. Next to it, drag and drop a Textbox control, set its ID to tb_pwd and its TextMode to Password. Again, add a RequiredFieldValidator, set its ErrorMessage property to Password cannot be blank and its ControlToValidate property to tb_pwd. Over the control added above, put a RegularExpressionValidator, set its ErrorMessage property to Password must be at least 5 characters long., set its ControlToValidate property to tb_pwd and its ValidationExpression to “^[w]{5,}”. For the next row, add a Label control with the Text set to “Retype Password”. Add a Textbox and set its ID to tb_retypePwd. Next to the Textbox, add a CompareValidator. We will use this control to compare the two passwords entered in textboxes. If they are not equal, again we display the error message. Let’s set the ErrorMessage property to Passwords don’t match. Now set the ControlToValidate property to tb_retypePwd and the ControlToCompare property to tb_pwd. Again add a Label control and set its Text property to E-mail. Add a Textbox and set its ID to tb_email. Let’s add a RequiredFieldValidator and place it next to the Textbox. Set its ErrorMessage to The e-mail address is required and its ControlToValidate property to tb_email. Lastly, add a RegularExpressionValidator and set its ControlToValidate property to tb_email. For the ValidationExpression property, choose Internet E-Mail Address.

         Now let’s add two buttons, first one with the Text property set to Submit and it’s ID set to btn_submit and the second one with the Text set to Clear Form and the ID set to btn_clearForm.

         After taking these steps, my web form looks like this:

         For the coding part, it will not require lots of coding, only display a short text when the user completed the form and pressed the Submit button. If he presses Clear Form, we want to clear all the textboxes.
So, let’s double-click the Submit button. Visual Studio .NET will create for us a method called btn_submit_Click. In this method, we will add the following code:

if (Page.IsValid)
{
Response.Write(“<h2>Congratulations, registration succeded!</h2>”);
}

         That’s it for this method. Go back to the Design view and double-click the Clear Form button. In the newly created method, add the following code to clear the textboxes:

tb_userName.Text = “”;
tb_pwd.Text = “”;
tb_retypePwd.Text = “”;
tb_email.Text = “”;

         Here is the form in action on my computer:

         And that’s about it.
Until next time, I wish you good luck and happy programming.
Raul POPESCU


Attachments:

   Project Files: asp_net_validation_basic.zip