Introduction
In this blog, I will explain the Validators in ASP.Net.The validation control is used to implement a page level validation in server controls.
Types
- RequiredFieldValidator
- RangeValidator
- RegularExpressionValidator
- CompareValidator
- ValidationSummary
- CustomValidator
RequiredFieldValidator
Indicates the input Control is not empty when the form is submitted.
Example
- <asp:TextBox ID="txtFname" runat="server" Height="25" Width="125" placeholder="Fname"></asp:TextBox>
- <asp:RequiredFieldValidator ID="ReqFldvalidator" runat="server" ValidationGroup="ValidGrp" ControlToValidate="txtFname" ErrorMessage="Enter the FirstName" ForeColor="Red"></asp:RequiredFieldValidator>
- <asp:Button ID="BtnSubmit" runat="server" Height="25 " Width="125" Text="click" OnClick="BtnSubmit_Click" ValidationGroup="ValidGrp" />
RangeValidator
The Rangevalidator control is verifies that the input value falls within a given range of number, date or string.
Example
- <asp:TextBox ID="txtage" runat="server" Height="25" Width="125" placeholder="Age"></asp:TextBox>
- <asp:RangeValidator ID="RangeValidator" runat="server" ValidationGroup="ValidGrp" ControlToValidate="txtage" MinimumValue="18" MaximumValue="35" ForeColor="Red" ErrorMessage="Enter the 18-35 age only "></asp:RangeValidator>
- <asp:Button ID="BtnSubmit" runat="server" Height="25 " Width="125" Text="click" OnClick="BtnSubmit_Click" ValidationGroup="ValidGrp" />
RegularExpressionValidator
The RegularExpressionValidator control validates the input text of a control that matches a pattern defined by a RegularExpression.
Example
- <asp:TextBox ID="txtPhone" runat="server" Height="25" Width="125" placeholder="Phone"></asp:TextBox>
- <asp:RegularExpressionValidator ID="RegExpValidator" runat="server" ValidationExpression="(0\d{1,4}-|\(0\d{1,4}\) ?)?\d{1,4}-\d{4}" ValidationGroup="ValidGrp" ControlToValidate="txtPhone" ForeColor="Red" ErrorMessage="Enter the valid phoneNo"></asp:RegularExpressionValidator>
- <asp:Button ID="BtnSubmit" runat="server" Height="25 " Width="125" Text="click" OnClick="BtnSubmit_Click" ValidationGroup="ValidGrp" />
CompareValidator
The Comaprevalidator control compares the value of one control to another using a comparison operator.
Example
- <asp:TextBox ID="txtNumber" runat="server" Height="25" Width="125" placeholder="Number "></asp:TextBox>
- <asp:TextBox ID="txtCompareNumber" runat="server" Height="25" Width="125" placeholder="CompareNumber"></asp:TextBox>
- <asp:CompareValidator ID="Cmpvalidator" runat="server" ValidationGroup="ValidGrp" ControlToValidate=" txtNumber " ValueToCompare=" txtCompareNumber" ForeColor="Red" ErrorMessage="Invalid Compare value"></asp:CompareValidator>
- <asp:Button ID="BtnSubmit" runat="server" Height="25 " Width="125" Text="click" OnClick="BtnSubmit_Click" ValidationGroup="ValidGrp" />
ValidationSummary
The ValidationSummary control doesn’t perform any validation. Instead, it shows a summary of errors raised by each control on the page.
Example
- <asp:ValidationSummary ID="VldSummry" runat="server" ValidationGroup="ValidGrp" ForeColor="Red" />