0

Hi,

I want sample code to validate the dates that is to be in the format YYYY-MM-DD(2011-04-21).

I had created two Textboxes to enter date and one Button Control.

The date format to be entered in the Textbox should be exactly like
YYYY-MM-DD(2011-04-21).

If user enters any other format like dd-mm-yyyy or mm-dd-yyyy or dd/mm/yyyy or mm/dd/yyyy and clicks the button it has to show error message in the label like
'Please enter valid date format'.

I'm not getting it how to do.

Can anyone please help me on this.

With Thanks,
Pavan Kumar R

2
Contributors
4
Replies
6
Views
6 Years
Discussion Span
Last Post by pavankumarr
0

Use DateTime.TryParseExact method,

DateTime dt;
        DateTime.TryParseExact(TextBox1.Text, "yyyy-MM-dd", null, System.Globalization.DateTimeStyles.None, out dt);

        if (dt == DateTime.MinValue)
            Label1.Text = "Invalid";
        else
            Label1.Text = "Valid";
0

Hi KV(adatapost),

Thanks for Your reply.

But I'm not getting what you answered.

In my Program i have the aspx page where i created the two text box control and one button control.

If i click the button after entering the date values in the From(Date) and To(Date) Textboxes correctly ,the aspx will redirect to a new .ashx page,

Otherwise if the dates are wrong it has to show error message.

One more query is that::
If i enter the date value greater than the value of a month(i.e.if I enter date as 2011-04-31(YYYY-MM_DD) ) it has to show the error as "Invalid Date"


Thanks,

0

pavankumarr

The code I've posted is to validate a date. You may use Session, cookies or query string to pass values between requests.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.