ASP.NET's validation controls validate user-entered data to ensure that useless, unauthorized, and contradictory data cannot be stored.
ASP.NET provides the following aspects of validation control:
RequiredFieldValidator
RangeValidator
CompareValidator
Regular expression validator (RegularExpressionValidator)
Custom validator (CustomValidator)
Validation summary control (ValidationSummary)
Validation classes inherit from the BaseValidator class, so they inherit its properties and methods. Therefore, learning the properties and methods of this basic class, which is the basis of all effectiveness controls, will be of great help to subsequent learning:
components | describe |
---|---|
ControlToValidate | Gets or sets the input control to be validated. |
Display | Describe how error messages are displayed. |
EnableClientScript | Indicates whether the client has adopted verification. |
Enabled | Enable or disable the validator. |
ErrorMessage | A string describing the error. |
Text | Text to be displayed if validation fails. |
IsValid | Indicates whether the control value is valid. |
SetFocusOnError | Whether to set focus to the relevant input control when validation fails. |
ValidationGroup | Gets or sets the name of the validation group to which this validation control belongs. |
Validate | Performs validation on the associated input control and updates the IsValid property. |
The RequiredFieldValidator control ensures that required fields are not empty. It is mainly bound to the text box to allow the user to input into the text box.
The syntax for this control is as follows:
<asp:RequiredFieldValidator ID="rfvcandidate" runat="server" ControlToValidate ="ddlcandidate" ErrorMessage="Please choose a candidate" InitialValue="Please choose a candidate"></asp:RequiredFieldValidator>
The RangeValidator control is responsible for verifying whether the entered value is within the preset range.
It has three specific properties:
property | describe |
---|---|
Type | It defines the data type. Available data types include: Currency, Date, Double, Integer, and String |
MinimumValue | It specifies the minimum value in the range |
MaximumValue | It specifies the maximum value in the range |
The syntax of this control is as follows:
<asp:RangeValidator ID="rvclass" runat="server" ControlToValidate="txtclass" ErrorMessage="Enter your class (6 - 12)" MaximumValue="12" MinimumValue="6" Type="Integer"></asp:RangeValidator>
The CompareValidator control validates a value based on the value entered into another input control, a constant numeric value, or the correct data type.
It has the following specific properties:
property | describe |
---|---|
Type | It defines the data type. |
ControlToCompare | It specifies the value in the input control that needs to be compared. |
ValueToCompare | It specifies a value that does not change in the input control. |
Operator | It specifies the comparison operator. Available values include: equality, inequality, greater than or equal to, less than, less than or equal to, and data type checking. |
The basic syntax of this control is as follows:
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="CompareValidator"></asp:CompareValidator>
The RegularExpressionValidator control allows you to determine the validity of input by matching it with a regular expression. The regular expression is set in the ValidationExpression property.
The following table summarizes the syntax structures commonly used in regular expressions:
escape character | describe |
---|---|
b | Matches the backspace key. |
t | Matches tab. |
r | Matches the Enter key. |
v | Matches vertical tab characters. |
f | Matches the form feed character. |
n | Match newlines. |
escape character. |
In addition to simple character matching, a type of characters can be set to match. These characters are called wildcards.
wildcard | describe |
---|---|
. | Can match any character except n. |
[abcd] | Can match any character in the set. |
[^abcd] | Exclude any character from the set. |
[2-7a-mA-M] | Matches any character within a specific range. |
w | Matches any alphanumeric character group and the underscore. |
W | Matches any non-word character. |
s | Match characters such as spaces, tab stops, newlines, etc. |
S | Matches any non-space character. |
d | Matches any decimal character. |
D | Matches any non-decimal character. |
Quantifiers can indicate a specific number of words in which a character occurs.
quantifier | describe |
---|---|
* | Zero or more matches. |
+ | One or more matches. |
? | Zero or one match. |
{N} | N matches. |
{N,} | N or more matches. |
{N,M} | Match between N and M. |
The basic syntax of this control is as follows:
<asp:RegularExpressionValidator ID="string" runat="server" ErrorMessage="string" ValidationExpression="string" ValidationGroup="string"></asp:RegularExpressionValidator>
The CustomValidator control allows writing client- and server-side specific validation routines to validate values.
Client validation is done appropriately via ClientValidationFunction. Client-side validation routines should be written in a scripting language that the browser understands, such as JavaScript or VBScript.
Server-side validation routines should be generated by the control's ServerValidate event handler. Server-side validation routines should be written in any .Net language, such as C# or VB.Net.
The basic syntax of this control is as follows:
<asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction=.cvf_func. ErrorMessage="CustomValidator"></asp:CustomValidator>
The ValidationSummary control does not perform any validation but displays a summary of all errors on the page. This summary displays the values of the ErrorMessage property of all failed validation controls.
The following two mutually inclusive attribute lists list error messages:
ShowSummary : Display error information in a special format.
ShowMessageBox : Display error messages in a separate window.
The basic syntax of this control is as follows:
<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode = "BulletList" ShowSummary = "true" HeaderText="Errors:" />
Complex pages will have different sets of information at different levels. In this case, different groups will have different verifications. This situation can be solved by using verification groups.
To create a validation group, you must place the input control and validation control in the same logical group by setting their ValidationGroup properties.
The following example describes a four-part form that will be filled out by the entire school to run for president. Here, we will use validation controls to validate what the user has entered.
This is what it looks like in Design view:
The code for this part is as follows:
<form id="form1" runat="server"> <table> <tr> <td colspan="3" align="center"> <asp:Label ID="lblmsg" Text="President Election Form : Choose your president" runat="server" /> </td> </tr> <tr> <td> Candidate: </td> <td> <asp:DropDownList ID="ddlcandidate" runat="server" > <asp:ListItem>Please Choose a Candidate</asp:ListItem> <asp:ListItem>M H Kabir</asp:ListItem> <asp:ListItem>Steve Taylor</asp:ListItem> <asp:ListItem>John Abraham</ asp:ListItem> <asp:ListItem>Venus Williams</asp:ListItem> </asp:DropDownList> </td> <td> <asp:RequiredFieldValidator ID="rfvcandidate" runat="server" ControlToValidate ="ddlcandidate" ErrorMessage="Please choose a candidate" InitialValue="Please choose a candidate"> </asp:RequiredFieldValidator> </td> </tr> <tr> < td> House: </td> <td> <asp:RadioButtonList ID="rblhouse" runat="server" RepeatLayout="Flow"> <asp:ListItem>Red</asp:ListItem> <asp:ListItem>Blue</asp:ListItem> <asp:ListItem>Yellow</asp:ListItem> <asp:ListItem>Green</asp:ListItem> </ asp:RadioButtonList> </td> <td> <asp:RequiredFieldValidator ID="rfvhouse" runat="server" ControlToValidate="rblhouse" ErrorMessage="Enter your house name" > </asp:RequiredFieldValidator> <br /> </td> </tr> <tr> <td> Class: </td> <td> <asp:TextBox ID="txtclass " runat="server"></asp:TextBox> </td> <td> <asp:RangeValidator ID="rvclass" runat="server" ControlToValidate="txtclass" ErrorMessage="Enter your class (6 - 12)" MaximumValue="12" MinimumValue="6" Type="Integer"> </asp:RangeValidator> </td> </tr> <tr> <td> Email: </td> <td> <asp:TextBox ID="txtemail" runat="server"> </asp:TextBox> </td> <td> <asp:RegularExpressionValidator ID="remail" runat="server" ControlToValidate="txtemail" ErrorMessage="Enter your email" ValidationExpression="w+([-+.']w+)*@w+([ -.]w+)*.w+([-.]w+)*"> </asp:RegularExpressionValidator> </td> </tr> <tr> <td align="center" colspan="3"> <asp:Button ID="btnsubmit" runat="server" onclick="btnsubmit_Click" style="text-align: center" Text= "Submit" /> </td> </tr> </table> <asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode ="BulletList" ShowSummary ="true" HeaderText="Errors:" /></form>
The code for the submit button is as follows:
protected void btnsubmit_Click(object sender, EventArgs e){ if (Page.IsValid) { lblmsg.Text = "Thank You"; } else { lblmsg.Text = "Fill up all the fields"; }}