Free Web Development Online Tutorials, Learn to Code
Validation Control in Asp.net, RequiredFieldValidator, RangeValidator, CompareValidator, RegularExpressionValidator

Asp.net Validation Control Examples

In this Asp.net validation control tutorial, you learn how to use different validation controls in asp.net 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">
</asp:RangeValidator>

Look at the property value of Type, MinimumValue & MaximumValue

CompareValidator in Asp.net 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">   
</asp:CompareValidator>
  • 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 asp.net 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 Asp.net

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

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.

Comment
Name
Email
Website
Subscribe
 
learn asp.net

hire remote asp.net developer
learn aspnet mvc
Asp.net Validation Control example- Form Validation in Asp.net
Asp.net Interview Questions Answers
Asp.Net C# Examples | Join Asp.Net Course | Learn Asp.net MVC | Asp.net Core Tutorial