What is the RequiredFieldValidator?
The required field validator is an ASP.NET control that allows us to validate that a field has data before continuing. The required field validator uses server side validation to check a specified control for data. This validator is both extremely simple to use and powerful. An example of where you would use this would be for something like a user registration, in which you have a text box for the user to enter a username that is required to go on.
We moved our web sites to Server Intellect
and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.
Using the RequiredFieldValidator
To demonstrate this control, we will create a simple web site in which we will have a text box and button. The button will initiate validation on the text box and our required field validator will determine if an error needs to be displayed or if we can move on. To do this, create a new ASP.NET Empty Web Site and:
- Right click the project in your solution explorer.
- Select add new item...
- Select a web form.
- Name it 'Default.aspx'.
- Click add.
- Open Default.aspx up to design mode.
- Drag and drop a textbox onto the web form.
- Drag and drop a requiredfieldvalidator onto the web form.
- Change the ErrorMessage property to 'Text box is required!'.
- Change the ControlToValidate property to 'TextBox1'.
- Add a break line.
- Drag and drop a button onto the web form.
What this has done, is associated the text box we added with the required field validator. In this case, it should display an error message if the text box is empty and the button is clicked.
If you're ever in the market for some great Windows web hosting
, try Server Intellect
. We have been very pleased with their services and most importantly, technical support.
To test this out, simply load up the web site and click the button. Notice that the error message we specified earlier is displayed. The text box did not pass validation because it is required to contain some sort of data. Go ahead and add some text to the text box and then click the button, notice the error message was not displayed meaning it passed validation.