Professional Documents
Culture Documents
BRANCH- CS&IT
SEMESTER- Vth
SUBJECT – ASP.NET
Validation Control :-
It is a process of checking weather user is entered correct data or correct input or not.
(i) Client Side Validation :- In a browser at client side weather user is entered data is correct or not will check out.
(ii) Server Side Validation :- After entering the data in web page, after sending request to the server its server side it will
be checked weather user entered the data correct or not.
(ii) Regular Expression Validator To check weather user enter the data is in correct format
or not.
(iii) Compare Validator Compare validator is use to compare the data which is entered in one text box with comparing
other text box.
(iv) Range Validator Range Validator is use to check weather user is entered correct range of
value or not.
(v) Custom Validator Custom validator is only one validation control which is used to perform both side validation (ie.
Client side, Server side). For the client side validation we use javaScript functionality which is also called client validation
property.
But if we want to perform server side validation using custom validation, server validate event will be raised.
(vi) Validation Summary It not perform any validation rather this validation summary control will display all error
messages of remaining validation controls at a time together.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace RequiredFieldValidation
{
public partial class requiredField : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
When we take the input and submit it then page is submitted message will be display.
Regular Expression Validator
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace RegularExpressionValidator
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
When we take wrong input/format then this type of validation will be occurred.
(Q) WAP to check the password entered by the user is correct or not, if the password is correct then page
will be submitted.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace compareValidator
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Range Validation :-
(Q) WAP to validate the age entered by the user is between 1 to 120 by using range validation :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace rangeValidation1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
lblDisplay.Text="Page is submitted Successfully";
}
}
}
(Q) WAP to check the number is even, i.e display the message (Must be Even) by using custom validation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace rangeValidation1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
lblDisplay.Text="Page is submitted Successfully";
}
}
}
When user take odd number:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" lang="ja"> // JavaScript function
function IsOdd(source, args) {
if (args.Value == "") {
args.IsValid = false;
}
else {
if (args.Value % 2 == 0) {
args.IsValid = false;
}
else {
args.IsValid = true;
}
}
}
</script>
</head>
<body style="height: 235px; width: 1963px">
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Font-Bold="True" style="z-index: 1; left: 11px; top:
9px; position: absolute" Text="Enter Number :"></asp:Label>
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtNumber"
Font-Bold="True" ForeColor="#CC0000" style="z-index: 1; left: 384px; top: 25px; position:
absolute" ClientValidationFunction="IsOdd">Number must be odd</asp:CustomValidator>
<p>
<asp:Button ID="Button1" runat="server" Font-Bold="True" style="z-index: 1; left: 51px;
top: 86px; position: absolute; width: 74px" Text="Submit" OnClick="Button1_Click" />
<asp:TextBox ID="txtNumber" runat="server" style="z-index: 1; left: 201px; top:
12px; position: absolute; height: 35px; width: 143px"></asp:TextBox>
</p>
<p>
</p>
<p>
<asp:Label ID="lblDisplay" runat="server" Font-Bold="True" style="z-index: 1; left: 154px;
top: 103px; position: absolute" ForeColor="#CC0000"></asp:Label>
</p>
</form>
</body>
</html>
// Exectution code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace evenCustom
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Ans:-
//Execution code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace customValidationServerSide
{
public partial class customServer : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Design code
//source
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
#form1 {
height: 248px;
width: 1546px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="height: 261px; width: 1549px">
<asp:Button ID="Button1" runat="server" Font-Bold="True" Font-Size="X-Large"
OnClick="Button1_Click" style="z-index: 1; left: 62px; top: 112px; position: absolute;
width: 119px; height: 39px" Text="Submit" />
<asp:Label ID="Label2" runat="server" Font-Bold="True" Font-Size="X-Large" style="z-index:
1; left: 21px; top: 28px; position: absolute; width: 201px" Text="Enter
Password"></asp:Label>
<asp:TextBox ID="txtPassword" runat="server" Font-Bold="True" style="z-index: 1; left:
257px; top: 25px; position: absolute; width: 193px; height: 40px"></asp:TextBox>
<asp:Label ID="lblDisplay" runat="server" Font-Bold="True" Font-Size="X-Large"
ForeColor="Red" style="z-index: 1; left: 224px; top: 155px; position: absolute; height:
37px; width: 152px"></asp:Label>
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtPassword"
Font-Bold="True" Font-Size="X-Large" ForeColor="#CC0000"
OnServerValidate="CustomValidator1_ServerValidate" style="z-index: 1; left: 491px; top:
51px; position: absolute; width: 438px">Password minimum six
character</asp:CustomValidator>
</div>
</form>
</body>
</html>
//output
If password not is in six character
// Execution code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace validationSummary
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// source
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
height: 26px;
}
.auto-style3 {
z-index: 1;
left: 231px;
top: 301px;
position: absolute;
}
.auto-style4 {
z-index: 1;
left: 144px;
top: 284px;
position: absolute;
width: 82px;
}
.auto-style5 {
z-index: 1;
left: 252px;
top: 56px;
position: absolute;
width: 185px;
height: 36px;
}
.auto-style6 {
width: 1559px;
height: 44px;
position: absolute;
top: 167px;
left: 10px;
z-index: 1;
}
.auto-style7 {
z-index: 1;
left: 447px;
top: 96px;
position: absolute;
}
.auto-style8 {
z-index: 1;
left: 250px;
top: 106px;
position: absolute;
width: 182px;
height: 30px;
}
.auto-style9 {
z-index: 1;
left: 249px;
top: 17px;
position: absolute;
height: 25px;
width: 188px;
}
.auto-style10 {
z-index: 1;
left: 15px;
top: 113px;
position: absolute;
}
.auto-style11 {
z-index: 1;
left: 13px;
top: 69px;
position: absolute;
}
.auto-style12 {
z-index: 1;
left: 13px;
top: 25px;
position: absolute;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtPassword" runat="server" CssClass="auto-style9" Font-
Bold="True"></asp:TextBox>
<table class="auto-style1">
<tr>
<td>
<asp:Label ID="Label1" runat="server" CssClass="auto-style12" Font-Bold="True" Text="Enter
Password"></asp:Label>
</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" CssClass="auto-style11" Font-Bold="True" Text="Enter
Email Id"></asp:Label>
<asp:TextBox ID="txtPhone" runat="server" CssClass="auto-style8"></asp:TextBox>
<asp:TextBox ID="txtEmail" runat="server" CssClass="auto-style5"></asp:TextBox>
</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style2">
<asp:Label ID="Label3" runat="server" CssClass="auto-style10" Font-Bold="True" Text="Enter
Phone Number"></asp:Label>
</td>
<td class="auto-style2">
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ControlToValidate="txtPassword" ErrorMessage="Password minimum 6 character" Font-
Bold="True" ForeColor="Red" style="z-index: 1; left: 449px; top: 21px; position: absolute"
ValidationExpression="\w{6}">*</asp:RegularExpressionValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server"
ControlToValidate="txtEmail" ErrorMessage="Email Id must be email-id format" Font-
Bold="True" ForeColor="#CC0000" style="z-index: 1; left: 448px; top: 57px; position:
absolute" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</
asp:RegularExpressionValidator>
</td>
<td class="auto-style2"></td>
<td class="auto-style2"></td>
<td class="auto-style2"></td>
<td class="auto-style2"></td>
</tr>
<tr>
<td> </td>
<td>
<asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server"
ControlToValidate="txtPhone" CssClass="auto-style7" ErrorMessage="Phone number must be 10
digit" Font-Bold="True" ForeColor="#CC0000"
ValidationExpression="/d{10}">*</asp:RegularExpressionValidator>
</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style2"></td>
<td class="auto-style2"></td>
<td class="auto-style2"></td>
<td class="auto-style2"></td>
<td class="auto-style2"></td>
<td class="auto-style2"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
</div>
<p>
<asp:Label ID="lblDisplay" runat="server" CssClass="auto-style3" Font-Bold="True"
ForeColor="#CC0000"></asp:Label>
<asp:Button ID="Button1" runat="server" CssClass="auto-style4" Font-Bold="True"
OnClick="Button1_Click" Text="Submit" />
</p>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" CssClass="auto-style6" Font-
Bold="True" ForeColor="#CC0000" HeaderText="Invalid enteries in this page" />
</form>
</body>
</html>
// Design Code
// OUTPUT
------------------------------------------------------------------THANK YOU--------------------------------------------------------------------------------------------------