Tutorials » AspNet tutorials »

Validate name in Login page using RequiredFieldValidator control

In this chapter, you will learn what are validator controls and how to use them in the UserTracker project.

Validate name in Login page

Validator controls are used to validate user inputs from a webpage.

For example, when you have a webpage which requires that user must enter some data, you can use a "Require Field Validator" control to ensure that the form will not be sumitted until some valid data is entered.

In our UserTracker project, user is required to enter a name in the login page. But you have noticed that even if you do not enter a name and press the "proceed" button, it will not complain. It will still process the login and save the values in to Cooikes etc.

This is not good. We want to make sure that the Login page will not be processed until they enter a name.

Open the Login.aspx page in design mode. Drag and drop a control called "RequiredFieldValidator" from the toolbox to the form. Place this validator control near the textbox.

You must set the following properties for the validator control.

1. ErrorMessage - This is the error message you want to be displayed to the user, if no value is entered in the textbox. Set it to "Please enter your name."

2. ControlToValidate - This property says which control will be validated using this validator. In our case, we want to validate our textbox. So, select the textbox name for this property.

Thats all you have to do use the validator control. Compile and run the project. You can see that you will get an error message if you submit the form without entering a name.

Client side validation

Note that when you use Validator controls, the validation happens in the client side. Since this is happening in the client browser, a smart user can by pass this validation and still submit empty names to the server by various tricks.

So, in case of secure applications, it is a good idea to do server side validations, in addition to the validator controls. In case of our sample, all you have to do is, check if the name string is empty or not just before it is processed. If it is empty, then do not process the login. Just give an error message to the user (This is not illustrated in the sample project).

Download sample project

You can download the sample from here. Please submit your feedback about this project.

Next Chapter: Validator Controls in ASP.NET
Previous Chapter: Using Cookies to remember user settings
More Chapters: ASP.NET Tutorials
More Tutorials: Tutorial Index

Top Contributors
    Last 7 Daysmore...

    Awards & Gifts

    Online Members

    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India