Required Field Validation Controls


One of the most tiresome tasks when building interactive web applications is the requirement for validating values that the user enters into the input controls. This is particularly the case if we need to perform client-side as well as server side validation. Mostly, we use JavaScript for client side coding. Help is at hand with the range of validation controls that are included in ASP.NET.

They cover almost all the validation scenarios. A validation control enables us to validate an input and display an error message if necessary. It is very much like other server-side controls, with certain additional methods and properties. First, the server treats it as an invisible control. After the user has entered erroneous data, it becomes visible. It is a powerful, rapid application development feature; however, a developer needs to understand its behavior and the methods thoroughly before he or she can appreciate it. All the validation controls inherit from the base class
BaseValidator, which is part of the class library namespace. System.Web.UI.WebControls.BaseValidator exposes a series of properties and methods that are common to all the validation controls.

RequiredFieldValidator

<asp:RequiredFieldValidator>

Checks that the validated control contains a value. It cannot be empty. Can be used in conjunction with other validators on a control to trap empty values. Simply, we can use it to check if the input control has any value. The most important property in the
RequiredFieldValidator is InitialValue.

<asp:TextBox id="txtName" runat="server"  />
<asp:RequiredFieldValidator id="validTxtName" runat="server"  controlToValidate="txtName"
errorMessage="Please enter Your Name" display="static">
</asp:RequiredFieldValidator>

Summary

In this article I explained the basic concepts and usage of Validation controls in ASP.NET.