Exforsys

H I D E

Home arrow Technical Training arrow ASP.NET Training

Required Field Validator

Page 2 of 7
Author : Exforsys Inc.     Published on: 4th Mar 2005    |   Last Updated on: 18th Dec 2010

ASP .NET: Validating User Input with C#

Required field validator is used for required fields. Fields that cannot be left empty is validated using the required field validator. A good example will be a TextBox which is used for taking the username as the input. Since the user must enter the username in the TextBox in order to access the website. The required field validator can be used with the TextBox control to validate the input. If user does not enter any data in the TextBox than an error message is displayed and further processing of the request will be stopped. In order to set the Required validator on the TextBox control just drag and drop the validator on the webform and set its ControltoValidate property to the TextBox id you want to validate.

Sample Code
  1. private void Button1_Click(object sender, System.EventArgs e)
  2. {
  3. if(Page.IsValid == true)
  4. {
  5. // Do the processing here
  6. }
  7. }
Copyright exforsys.com


The Page.IsValid property is true if the page does not have any error messages to display. The Required field validator performs the validation first on the client side and than on the server side.

Read Next: Using Rich Server Controls with C#


 
This tutorial is part of a ASP.NET Training tutorial series. Read it from the beginning and learn yourself.

ASP.NET Training

 

Comments