ASP.NET – Validators
ASP.NET – Validators
Validation controls enable you to rapidly create Web Forms with validation rules that prohibit users from entering invalid data. For example, you can force users to enter values for required fields and check whether the entered data matches a specific format like a valid date or a number between 1 and 10. ASP.NET provides the following validation controls:
1) RequiredField validator
2) Compare Validator
3) Range Validator
4) RegularExpression Validator
5) Custom Validator
6) Validation summary
REQUIREDFIELD VALIDATOR:
The RequiredFieldValidator class ensures that text has been entered into a TextBox control or FileUpload control or that the text in one of these controls has been changed from some specified initial value.
It also ensures that a selection has been made from a DropDownList control, ListBox control, or RadioButtonList control or that the selection has been changed from some specified initial value.
The InitialValue property of a RequiredFieldValidator control indicates the text or selection that we do not want the end user to enter into or select from an input control.
Thus, an error message will be displayed when an input control contains the same value as its associated RequiredFieldValidator control’s InitialValue property.
COMPARE VALIDATOR
The CompareValidator class compares the value entered into one input control (e.g., a TextBox control) with the value entered into a second input control.
It also compares the value entered into an input control with a constant (e.g., zero) or ensures that the value entered into an input control is of a specific type (e.g., date).
The ControlToCompare property of a CompareValidator control indicates the input control to be compared to the input control being validated. The Operator property indicates the type of comparison operation to be performed (i.e., equal to, not equal to, greater than, greater than or equal to, less than, less than or equal to, or data type check). The Type property indicates the kind of values being compared (i.e., currency, date, double, integer, or string).
The ValueToCompare property indicates the value to be compared to the input control being validated.
RANGE VALIDATOR
The RangeValidator class ensures that the value entered into an input control (e.g., a TextBox control) lies within a range of acceptable values.
The MinimumValue property of a RangeValidator control indicates the minimum acceptable value that can be entered into an input control.
The MaximumValue property indicates the maximum acceptable value that can be entered into an input control.
REGULAREXPRESSION VALIDATOR
The RegularExpressionValidator class ensures that the value entered into an input control (e.g., a TextBox control) matches a pattern defined by a regular expression. This class is helpful when we want to check an input control for a predictable sequence of characters like those found in postal codes, phone numbers, and email addresses.
The ValidationExpression property of a RegularExpressionValidator control indicates the regular expression to be matched. A RegularExpressionValidator control displays an error message when the value in its associated input control does not match the pattern specified in the ValidationExpression property of the control.
CUSTOM VALIDATOR
The CustomValidator class ensures that the value entered into an input control (e.g., a TextBox control) passes some custom-defined validation criterion or criteria. This class is helpful when none of the other validation controls meet our needs.
We can define a CustomValidator control that executes on the client by writing JavaScript code in our .aspx file, or we can define a CustomValidator control that executes on the server by writing C# code in our code behind file.
A CustomValidator control displays an error message when the value in its associated input control does not pass the custom-defined validation criterion or criteria.
VALIDATION SUMMARY VALIDATOR
The ValidationSummary class displays a summary of the validation errors that occur on a Web page. A ValidationSummary control can display validation errors in the page itself, or it can display validation errors in a separate message box.
A ValidationSummary control is helpful when we want to display all validation errors in one location. The DisplayMode property of a ValidationSummary control indicates whether the validation summary is to be displayed in the form of a list, a bulleted list, or a paragraph.
The HeaderText property indicates the text to be displayed at the top of the validation summary. The ShowMessageBox property indicates whether or not the validation summary is to be displayed in a message box that is separate from the page itself.
IMPORTANT POINTS FOR VALIDATION CONTROLS
1) ControlToValidate property is mandatory to all validate controls.
2) One validation control will validate only one input control but multiple validate control can be assigned to a input control.
The following project illustrated all validation controls in ASP.NET
Design a web form as shown below in Design Page
In Source page write the following code for each controls to validate
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication5.WebForm1" %>
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label3" runat="server" Style="top: 241px; left: 70px; position: absolute;
height: 22px; width: 128px; bottom: 282px;" Text="Enter your email id:"></asp:Label>
<asp:Label ID="Label1" runat="server" Style="top: 54px; left: 74px; position: absolute;
height: 22px; width: 128px; right: 768px;" Text="Enter your name:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Style="top: 54px; left: 221px; position: absolute;
height: 22px; width: 128px; right: 396px;"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Style="top: 56px;
left: 378px; position: absolute; height: 22px; width: 128px" ErrorMessage="RequiredFieldValidator"
ControlToValidate="TextBox1">name is
mandatory </asp:RequiredFieldValidator>
</div>
<p>
<asp:Button ID="Button1" runat="server" Style="top: 311px; left: 267px; position: absolute;
height: 26px; width: 61px" Text="Submit" />
</p>
<asp:TextBox ID="TextBox3" runat="server" Style="top: 145px; left: 217px; position: absolute;
height: 22px; width: 131px" TextMode="Password"></asp:TextBox>
<p>
<asp:TextBox ID="TextBox2" runat="server" Style="top: 101px; left: 218px; position: absolute;
height: 22px; width: 131px" TextMode="Password"></asp:TextBox>
<asp:Label ID="Label4" runat="server" Style="top: 105px; left: 74px; position: absolute;
height: 22px; width: 128px" Text="Password"></asp:Label>
<asp:TextBox ID="TextBox5" runat="server" Style="top: 239px; left: 210px; position: absolute;
height: 22px; width: 134px"></asp:TextBox>
</p>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" Style="top: 98px;
left: 367px; position: absolute; height: 26px; width: 162px" ErrorMessage="password required"
ControlToValidate="TextBox2"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" Style="top: 145px;
left: 367px; position: absolute; height: 26px; width: 162px" ErrorMessage="password required"
ControlToValidate="TextBox3"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator1" runat="server" Style="top: 149px; left: 512px;
position: absolute; height: 26px; width: 162px" ErrorMessage="CompareValidator"
ControlToValidate="TextBox3" ValueToCompare="hello"></asp:CompareValidator>
<p>
<asp:Label ID="Label5" runat="server" Style="top: 148px; left: 71px; position: absolute;
height: 22px; width: 128px; bottom: 375px;" Text="Confirm Password"></asp:Label>
<asp:TextBox ID="TextBox4" runat="server" Style="top: 194px; left: 212px; position: absolute;
height: 22px; width: 140px"></asp:TextBox>
<asp:Label ID="Label6" runat="server" Style="top: 194px; left: 71px; position: absolute;
height: 22px; width: 128px; bottom: 329px;" Text="Enter your age:"></asp:Label>
</p>
<asp:RangeValidator ID="RangeValidator1" runat="server" Style="top: 194px; left: 365px;
position: absolute; height: 22px; width: 105px" ErrorMessage="RangeValidator"
ControlToValidate="TextBox4" MaximumValue="100" MinimumValue="18" Type="Integer"></asp:RangeValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" Style="top: 234px;
left: 366px; position: absolute; height: 22px; width: 177px"
ErrorMessage="RegularExpressionValidator" ControlToValidate="TextBox5"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</form>
</body>
</html>
If I don’t fill any fields and submit the form, Our validations controls will process as follows
If I typed mismatched passsword, then comparefield validator triggers out as shown below