Validation Control Examples

In this validation control tutorial, you learn how to use different validation controls in application development.

Different type of Form validation controls to validate user input data

Validation controls in ASP.NET:
  • RequiredFieldValidator
  • RangeValidator
  • CompareValidator
  • RegularExpressionValidator
  • CustomValidator
  • ValidationSummary

Properties of BaseValidator Class

Namespace : System.Web.UI.WebControls
All validation controls are inherited from abstract class BaseValidator : Label, IValidator

  • ControlToValidate

    Indicate the ID of the control to be validated.

  • Display

    Tells how the error message to be shown

  • EnableClientScript

    Indicates if client side validation will occur

  • ErrorMessage

    error message string

  • IsValid

    Indicates if the value of the control is valid

  • ValidationGroup

    logical group of multiple validators

  • Text

    if validation fails then the error string to be displayed

  • SetFocusOnError

    If validation fails, then the focus should go to the related input control

RequiredFieldValidator Control Example

The RequiredFieldValidator control mak sure that the required field is not empty, normally tied to a textbox control to force input into textbox.

<asp:TextBox ID="txtName" runat="server"> </asp:TextBox>
<asp:RequiredFieldValidator ID="rfvName" runat="server" ErrorMessage="Please enter Name" ControlToValidate="txtName"> </asp:RequiredFieldValidator>
RangeValidator Control Example

Range Validator is used when we want to validate the value within two range, normally used for datatype like number, date etc.

Here in example we are checking the age range should be within 18 to 30
Syntax of RangeValidator

<asp:TextBox ID="txtAge" runat="server"> </asp:TextBox>
<asp:RangeValidator ID="rvAge" runat="server" 
ErrorMessage="Age should be 18 to 30" ControlToValidate="txtAge" type="Integer" MinimumValue="18" MaximumValue="30">

Look at the property value of Type, MinimumValue & MaximumValue

CompareValidator in Example

CompareValidator control is used to compare a value with value of another control or a fixed value.

Here in example below we are comparing password and re-password is equal

Password <asp:TextBox ID="txtPass" runat="server"> </asp:TextBox>
Confirm  <asp:TextBox ID="txtPassRe" runat="server"> </asp:TextBox>
<asp:CompareValidator ID="cvPass" runat="server" ErrorMessage="Password should match" ControlToValidate="txtPass" ControlToCompare="txtPassRe" Operator="Equal">   
  • ControlToValidate

    Id of main control textBox

  • ControlToCompare

    Id of control to compare with

  • ValueToCompare

    if you want to compare with any fixed value

  • Operator

    Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual

RegularExpressionValidator in example

RegularExpressionValidator is used for validating the user input matching against a pattern of a regular expression

In example below we are checking if user has entered a valid email

<asp:TextBox ID="txtEmail" runat="server"> </asp:TextBox>
<asp:RegularExpressionValidator ID="revEmail" runat="server" ErrorMessage="Invalid Email" ControlToValidate="txtEmail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"> </asp:RegularExpressionValidator>

ValidationExpression is string property that contain the expression, and check if user input is matching the expression, if not then display the ErrorMessage

CustomValidator in

The CustomValidator control allows writing any type of validation logic in your .net code then calling that method in CustomValidator

<asp:TextBox ID="txtCustom" runat="server" ></asp:TextBox>
<asp:CustomValidator ID="cv" runat="server" ControlToValidate="txtCustom"
ErrorMessage="Custom Validator Error" OnServerValidate="cv_ServerValidate"> </asp:CustomValidator>
//here is the server side method
protected void cv_ServerValidate(object source,
ServerValidateEventArgs args) { if (args.Value == "webtrainingroom") args.IsValid = true; else args.IsValid = false; }

ValidationSummary control is for displaying all validation error in one place, simply place the ValidationSummary control in your form where you want all message to be displayed

<asp:ValidationSummary ID="vsForm" runat="server" 
DisplayMode = "BulletList" ShowSummary = "true" />

You may also read how validation in Asp.Net MVC works.

Hire Asp.Net Developer Validation Control example: Aspx Form Validation in Interview Questions Answers MVC interview Core question application development tutorials with real-time examples create web application with SQL database step-by-step.
Asp.Net C# Examples | Join Asp.Net Course | Learn MVC | Core Tutorial