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" %>
function validate(e)
{
var KeyID=e.keyCode;
if(KeyID==9)
{
ControlToValidate="TextBox1" OnServerValidate="CustomValidator1_ServerValidate"
ForeColor="Red">
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;
}
}
}
}
4 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Manas MohapatraPosted Aug 6, 2015, 2:00 AM
SreenishPosted Aug 6, 2015, 1:59 AM
SreenishPosted Aug 6, 2015, 1:55 AM
Manas MohapatraPosted Aug 6, 2015, 1:49 AM