Validation is important part of any web application. User's input must always be validated before sending across different layers of the application.
Validation is of two types :
We can use ASP.NET validation, which will ensure client, and server validation. It work on both end; first it will work on client validation and than on server validation. At any cost server validation will work always whether client validation is executed or not. So you have a safety of validation check.
There are six types of validation controls in ASP.NET
The below table describes the controls and their work,
Validation Control | Description |
RequiredFieldValidation | Makes an input control a required field |
CompareValidator | Compares the value of one input control to the value of another input control or to a fixed value |
RangeValidator | Checks that the user enters a value that falls between two values |
RegularExpressionValidator | Ensures that the value of an input control matches a specified pattern |
CustomValidator | Allows you to write a method to handle the validation of the value entered |
ValidationSummary | Displays a report of all validation errors occurred in a Web page |
<div class="container">
<div class="form-group">
<label for="email">Name:</label>
<asp:TextBox ID="txtname" runat="server" CssClass="form-control"></asp:TextBox>
</div>
<div class="form-group">
<label for="email">Password:</label>
<asp:TextBox ID="txtpassword1" runat="server" CssClass="form-control"></asp:TextBox>
</div>
<div class="form-group">
<label for="email">Confirm Password:</label>
<asp:TextBox ID="txtpassword2" runat="server" CssClass="form-control"></asp:TextBox>
</div>
<div class="form-group">
<label for="email">Age:</label>
<asp:TextBox ID="txtage" runat="server" CssClass="form-control"></asp:TextBox>
</div>
<div class="form-group">
<label for="email">Email:</label>
<asp:TextBox ID="txtemail" runat="server" CssClass="form-control"></asp:TextBox>
</div>
<div class="form-group">
<label for="email">Username:</label>
<asp:TextBox ID="txtusername" runat="server" CssClass="form-control"></asp:TextBox>
</div>
<asp:Button ID="btnsubmit" runat="server" CssClass="btn btn-primary" Text="Submit" />
<div>
<div>
</div>
</div>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please Enter Name"
ControlToValidate="txtname"></asp:RequiredFieldValidator><br />
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Password Not Matches"
ControlToValidate="txtpassword1" ControlToCompare="txtpassword2"></asp:CompareValidator><br />
<asp:RangeValidator ID="RangeValidator1" runat="server" ErrorMessage="Please enter age between 18-100"
ControlToValidate="txtage" MaximumValue="100" MinimumValue="18" Type="Integer"></asp:RangeValidator><br />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ErrorMessage="Please Enter Valid email-id" ControlToValidate="txtemail"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
<br />
<asp:CustomValidator ID="CustomValidator1" runat="server" OnServerValidate="UserCustomValidate"
ControlToValidate="txtusername" ErrorMessage="User ID should have atleast a capital, small and digit and should be greater than 5 and less than 26 letters" SetFocusOnError="True"></asp:CustomValidator>
<hr />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Validations : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void UserCustomValidate(object source, ServerValidateEventArgs args)
{
string str = args.Value;
args.IsValid = false;
//checking for input length greater than 6 and less than 25 characters
if (str.Length < 6 || str.Length > 25)
{
return;
}
//checking for a atleast a single capital letter
bool capital = false;
foreach (char ch in str)
{
if (ch >= 'A' && ch <= 'Z')
{
capital = true;
break;
}
}
if (!capital)
{
return;
}
//checking for a atleast a single lower letter
bool lower = false;
foreach (char ch in str)
{
if (ch >= 'a' && ch <= 'z')
{
lower = true;
break;
}
}
if (!lower)
{
return;
}
bool digit = false;
foreach (char ch in str)
{
if (ch >= '0' && ch <= '9')
{
digit = true;
break;
}
}
if (!digit)
{
return;
}
args.IsValid = true;
}
}