ASP.NET validation controls validate user input data.
Validation controls are as follow:
- RequiredFieldValidator
This validator ensures the fields are not empty. It is bounded to the text box
- RangeValidator – this validator ensures the value put in the textbox/ label is within that range.
There are specific properties of the Range validator
- Type – defines the type of the data
- Minimum Range – defines the minimum range of values
- Maximum Range – defines the maximum range of values
- CompareValidator
This validator compares a value in one control to another. - Regular Expression validator
This ensures the input matching the pattern of the regular expression.
Examples:
- \b -matches backspace
- \t – matches tab
- \r – matches a return
- \v – matches a vertical tab
- Custom validator
This allows writing application-specific custom validation routines for both the client and server-side. - Validation summary
Validation summary control does not perform validation, it shows the summary of all errors on the page.
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Validation.aspx.cs" Inherits="Demo_1.Validation" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title> Form </title>
</head>
<body>
<form id="form1" runat="server">
<table style="width: 66%;">
<tr>
<td class="style1" colspan="3" align="center">
<asp:Label ID="lblmsg"
Text="Election Form"
runat="server" />
</td>
</tr>
<tr>
<td class="style3">
Candidates:
</td>
<td class="style2">
<asp:DropDownList ID="ddlcandidate1" runat="server" style="width:239px">
<asp:ListItem>Please Choose a Candidate</asp:ListItem>
<asp:ListItem>Donald Trump</asp:ListItem>
<asp:ListItem>Hilary Clinton</asp:ListItem>
<asp:ListItem>Joe Biden</asp:ListItem>
<asp:ListItem>Kamla Harris</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 class="style3">
Choose Color:
</td>
<td class="style2">
<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 favorite color" >
</asp:RequiredFieldValidator>
<br />
</td>
</tr>
<tr>
<td class="style3">
subject:
</td>
<td class="style2">
<asp:TextBox ID="txtclass" runat="server"></asp:TextBox>
</td>
<td>
<asp:RangeValidator ID="rvclass"
runat="server" ControlToValidate="txtclass"
ErrorMessage="Enter number of subjects between (6 - 12)" MaximumValue="12"
MinimumValue="6" Type="Integer">
</asp:RangeValidator>
</td>
</tr>
<tr>
<td class="style3">
Emailaddress:
</td>
<td class="style2">
<asp:TextBox ID="txtemail" runat="server" style="width:250px">
</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 class="style3" align="center" colspan="3">
<asp:Button ID="btnsubmit" runat="server" onclick="btnsubmit_Click"
style="text-align: center" Text="Submit" />
</td>
</tr>
</table>
</form>
</body>
</html>
// .aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Demo_1
{
public partial class Validation : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnsubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
lblmsg.Text = "Thank You";
}
else
{
lblmsg.Text = "Please fill the details";
}
}
}
}
Form