am using custom validation for date validation in textbox.i have stuck in if user enters leap year it is showing one error message like"29/02/2015" then if user enters invalid date is is showing same error message like"12/13/2015" format is dd/MM/yyy.so i want now different error messages .
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Dat n Txtbox.aspx.cs" Inherits="Validators.Validators_Task.Dat_n_Txtbox" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Custom Validator</title>
<script type="text/javascript">
function validate(e)
{
var KeyID=e.keyCode;
if(KeyID==9)
{
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" ForeColor="Red"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Date is INVALID "
ControlToValidate="TextBox1" OnServerValidate="CustomValidator1_ServerValidate"
ForeColor="Red"></asp:CustomValidator>
</div>
</form>
</body>
</html>
Code Behind::
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
namespace Validators.Validators_Task
{
public partial class Dat_n_Txtbox : System.Web.UI.Page
{
string dd = string.Empty;
string mm = string.Empty;
string yyyy = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
}
string[] get_date;
protected void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs e)
{
try
{
if (TextBox1.Text.Contains('/'))
{
get_date = TextBox1.Text.Split('/');
}
else
if (TextBox1.Text.Contains('-'))
{
get_date = TextBox1.Text.Split('-');
}
if (get_date.Length > 2)
{
dd = get_date[0].ToString();
mm = get_date[1].ToString();
yyyy = get_date[2].ToString();
int fourDigitYear = CultureInfo.CurrentCulture.Calendar.ToFourDigitYear(Convert.ToInt32(yyyy));
DateTime d = new DateTime(fourDigitYear, Convert.ToInt32(mm), Convert.ToInt32(dd));
TextBox1.Text = d.ToString("dd/MM/yyyy");
e.IsValid = DateTime.TryParseExact(TextBox1.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out d);
//TextBox1.Text = string.Empty;
}
}
catch (Exception ex)
{
//TextBox1.Text =string.Empty;
e.IsValid = false;
}
}
}
}