ASP.Net validation controls are used to validate the user input values using asp.net input controls.

The below properties are used to validate an input controls.

ControlToValidate:
Specifies which control to validate.

Display:
How the error message is shown in the UI.

EnableClientScript:
Whether client side validation will allow or not.

Enabled
Enable or disable the validator.

ErrorMessage:
Specifies the error message to display.

ASP.Net contains the below validation controls

  • RequiredFieldValidator
  • RangeValidator
  • CompareValidator
  • RegularExpressionValidator
  • CustomValidator
  • ValidationSummary

RequiredFieldValidator Control:

The RequiredFieldValidator Control is used to ensure the input is not empty.

Syntax:
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvName" runat="server" ErrorMessage="Name should not be blank" ControlToValidate="txtName"></asp:RequiredFieldValidator>

RangeValidator Control:

The RangeValidator Control is used to specify the range of the input. This need to specify maximum and minmum value.

Syntax:
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
<asp:RangeValidator ID="rvAge" runat="server" MaximumValue="60" MinimumValue="25" ErrorMessage="Invalid age"></asp:RangeValidator>

CompareValidator Control:

CompareValidator Control is used to compare two values, when we use password and confirm password we use this validator.

ControlToValidate and ControlToCompare properties are used to validate and compare two input values.

Syntax:
<asp:TextBox ID="txtPassword" TextMode="Password" runat="server"></asp:TextBox>
<asp:TextBox ID="txtCPassword" TextMode="Password" runat="server"></asp:TextBox>
<asp:CompareValidator ID="cvPassword" runat="server" ControlToValidate="txtPassword" ControlToCompare="txtCPassword" ErrorMessage="Password not match."></asp:CompareValidator>

RegularExpressionValidator:

RegularExpressionValidator is used to validate the input value by matching against a pattern of a regular expression. The regular expression is used to validate email, phone number, zip code etc.

The below syntax shows email format validation.

<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="revEmail" runat="server" ControlToValidate="txtEmail" ValidationExpression="^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,6}$" ErrorMessage="Invalid Email"></asp:RegularExpressionValidator>

CustomValidator:

CustomValidator is used to specify user defined validation, user can validate on his own requirement and write his own fuction for validation.

Syntax:
<asp:TextBox ID="txtAmount" runat="server"></<asp:TextBox>
<asp:CustomValidator ID="cusvAmount" ControlToValidate="txtAmount" runat="server" ClientValidationFunction="functionAmount()" ErrorMessage="Invalid Amount."></asp:CustomValidator>

User can write his own functionality and return a boolean value in the function functionAmount().

ValidationSummary:

The ValidationSummary control does not perform any validation but shows a summary of all errors in the page. The below properties need to specify in the validation summary.

ShowSummary - This will display the error message summary. 
ShowMessageBox - This property will show the error message in popup window.
DisplayMode - This property will display mode of display(List, BulletList).

Syntax:
<asp:ValidationSummary ID="ValidationSummaryMessage" ShowSummary="True" ShowMessageBox="True" DisplayMode="BulletList" runat="server" />

0 Comments1

Sorry, No more post.

Your Comment

Login To Comment

discard

By posting your Comment, you agree to the privacy policy and terms of service.