0% found this document useful (0 votes)
203 views7 pages

Lab Exercise - 9: Q1: Implement Custom Validation

This document contains code for implementing custom validation and validation summary controls in ASP.NET. For custom validation, it shows code for validating a textbox input on the server-side and client-side to check for a positive even number. For validation summary, it includes code to add multiple validation controls like required field, compare, and regular expression validators and displays a summary of validation errors.

Uploaded by

kuku288
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
203 views7 pages

Lab Exercise - 9: Q1: Implement Custom Validation

This document contains code for implementing custom validation and validation summary controls in ASP.NET. For custom validation, it shows code for validating a textbox input on the server-side and client-side to check for a positive even number. For validation summary, it includes code to add multiple validation controls like required field, compare, and regular expression validators and displays a summary of validation errors.

Uploaded by

kuku288
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

LAB EXERCISE 9

Q1: Implement custom validation


Server Side:<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<p> CUSTOM VALIDATION</p>
<form id="form1" runat="server">
<p>
Please enter a positive even number:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="Not a positive even
number" onservervalidate="CustomValidator1_ServerValidate">
</asp:CustomValidator>
</p> <p style="margin-left: 120px">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Submit" />
</p> <p style="margin-left: 120px">
<asp:Label ID="Label1" runat="server" ForeColor="#009933"></asp:Label>
</p> <div> </div>
</form>
</body>
</html>
Wbform1.aspx.cs
using System.*;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace WebApplication4
{ public partial class WebForm2 : System.Web.UI.Page
{
protected void CustomValidator1_ServerValidate(object source,
ServerValidateEventArgs args)
{
if (args.Value == " ")

{
args.IsValid = false;
}
else
{
int number;
bool isnumber = int.TryParse(args.Value, out number);
if(isnumber && number >=0 && number%2==0)
{
args.IsValid=true;
}
else
{
args.IsValid=false; } }
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
Label1.Text = "Data Saved";
Label1.ForeColor = System.Drawing.Color.Green;
else
{ Label1.Text = "Data not saved";
Label1.ForeColor = System.Drawing.Color.Red;
}}}}
Output:-

Client side:<html xmlns="http://www.w3.org/1999/xhtml" >


<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" language="javascript">
function iseven(source,args)
{
if(args.Value=="")
{
args.IsValid=false;
}
else
{
if(args.Value %2==0)
{
args.IsValid=true; }
else
{
args.IsValid=false; } } }
</script>
</head>
<body>
<p>
CUSTOM VALIDATION</p>
<form id="form1" runat="server">
<p>
Please enter a positive even number:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="Not a positive even
number"
onservervalidate="CustomValidator1_ServerValidate"
ValidateEmptyText="True"
ClientValidationFunction="iseven"></asp:CustomValidator>
</p><p style="margin-left: 120px">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Submit" />
</p>
<p style="margin-left: 120px">
<asp:Label ID="Label1" runat="server" ForeColor="#009933"></asp:Label>
</p><div></div>
</form>
</body>
</html>

Q2: Implement Validation summary Control


Webform1.aspx.cs
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<p style="margin-left: 160px">
VALIDATION SUMMARY</p>
<p>
USER NAME:
<asp:TextBox ID="TextBox2" runat="server"
Width="129px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox2" ErrorMessage="Please enter the User
name" Text="*"></asp:RequiredFieldValidator>

<br />
PASSWORD:
<asp:TextBox ID="TextBox3" runat="server"
TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox3" ErrorMessage="Please Enter the
password" Text="*"></asp:RequiredFieldValidator>
<br />
RE-TYPE PASSWORD:
<asp:TextBox ID="TextBox4" runat="server"
TextMode="Password"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToCompare="TextBox3" ControlToValidate="TextBox4"
ErrorMessage="Password do not match"
Display="Dynamic">*</asp:CompareValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="TextBox4" Display="Dynamic"
ErrorMessage="Please enter Re-Type
Password">*</asp:RequiredFieldValidator>
<br />
EMAIL:
<asp:TextBox ID="TextBox5" runat="server"
Width="128px"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server"
ControlToValidate="TextBox5" ErrorMessage="Enter a valid email id"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
Display="Dynamic">*</asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="TextBox5" Display="Dynamic"
ErrorMessage="Please enter the email id">*</asp:RequiredFieldValidator>
</p>
<div>
<asp:Button ID="Button1" runat="server" Text="SAVE"
onclick="Button1_Click" />
&nbsp;&nbsp;
<asp:Label ID="Label1" runat="server"></asp:Label>
<br />
<asp:ValidationSummary ID="ValidationSummary1" runat="server"

HeaderText="Page Errors" ShowMessageBox="True" />


</div>
</form>
</body>
</html>
Webform2.aspx.cs
using System.*;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace WebApplication4
{ public partial class WebForm3 : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{ if (Page.IsValid)
{ Label1.Text = "Data Saved";
Label1.ForeColor = System.Drawing.Color.Green; }
else
{ Label1.Text = "Data not saved";
Label1.ForeColor = System.Drawing.Color.Red;
} } } }

You might also like