0% found this document useful (0 votes)
71 views

Custom Validation Example

This document describes how to create a custom validator in ASP.NET to validate that a textbox entry is a positive even number. It includes a JavaScript function to validate on the client-side and a C# function to validate on the server-side. The validator checks if the value is empty, an integer, positive, and even. If validation passes, the form can be submitted and a success message is displayed. Otherwise, an error message appears.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Custom Validation Example

This document describes how to create a custom validator in ASP.NET to validate that a textbox entry is a positive even number. It includes a JavaScript function to validate on the client-side and a C# function to validate on the server-side. The validator checks if the value is empty, an integer, positive, and even. If validation passes, the form can be submitted and a success message is displayed. Otherwise, an error message appears.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Custom Validator Example

CustomValidator.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CustomValidation.aspx.cs"
Inherits="WebApplication1.CustomValidation" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" lang="ja">
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>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<b>Please Enter a Positive Even Number</b>
</td>
<td>
<asp:TextBox ID="txtEvenNumber" runat="server"
Width="150px"></asp:TextBox>
<asp:CustomValidator
ID="CustomValidator1"
runat="server"
ErrorMessage="Please Enter a Positive Even
Number!"
ForeColor="Red"
ControlToValidate="txtEvenNumber"
ClientValidationFunction="IsEven"
OnServerValidate="CustomValidator1_ServerValidate"
ValidateEmptyText="True"></asp:CustomValidator>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSave" runat="server"
Text="Save" Width="100px" OnClick="btnSave_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblStatus" runat="server"
Text=""></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
CustomValidator.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
public partial class CustomValidation : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

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 btnSave_Click(object sender, EventArgs e)


{
if (Page.IsValid)
{
lblStatus.ForeColor = System.Drawing.Color.Green;
lblStatus.Text = "Data Has Been Stored Successfully";
}
else
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "Validation Failed, Data Was Not Saved";
}
}
}
}

Note: before if (isNumber && Number>=0 && Number % 2 == 0)

protected void CustomValidator1_ServerValidate(object source,


ServerValidateEventArgs args)
{
if (args.Value == "")
{
args.IsValid = false;
}
else
{

if (Convert.ToInt32(args.Value) % 2 == 0)
{
args.IsValid = true;
}
else
{
args.IsValid = false;

}
}
}

You might also like