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;
}
}
}
}
Manas MohapatraRank #182 · 13.2K reputationPosted Aug 5, 2015, 9:49 PM EDT
SreenishRank #7,298 · 244 reputationPosted Aug 5, 2015, 9:55 PM EDT
SreenishRank #7,298 · 244 reputationPosted Aug 5, 2015, 9:59 PM EDT
Manas MohapatraRank #182 · 13.2K reputationPosted Aug 5, 2015, 10:00 PM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts