By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,916 Members | 1,286 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,916 IT Pros & Developers. It's quick & easy.

Date validation

P: n/a
Hi all a quick question: how can I validate a date in asp.net (2.0) with c#?
I didn't find a quick anwer.
Thanks, Diego.
Nov 17 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Hi Diego

Do you mean Client, or server side validation ?

kind regards

Ronnie

"Diego" wrote:
Hi all a quick question: how can I validate a date in asp.net (2.0) with c#?
I didn't find a quick anwer.
Thanks, Diego.

Nov 17 '05 #2

P: n/a
Possibly both :)
Thanks, Diego
"Ronnie Edgar" <Ronnie Ed***@discussions.microsoft.com> wrote in message
news:AA**********************************@microsof t.com...
Hi Diego

Do you mean Client, or server side validation ?

kind regards

Ronnie

"Diego" wrote:
Hi all a quick question: how can I validate a date in asp.net (2.0) with
c#?
I didn't find a quick anwer.
Thanks, Diego.

Nov 17 '05 #3

P: n/a
Use a custom validator for client side validation. with a datetime
validation script as the validationscript.

and server side

something like

try
{
datetime.parse (myVariable);
}
catch (exception ex)
{
throw;
}

"Diego" wrote:
Possibly both :)
Thanks, Diego
"Ronnie Edgar" <Ronnie Ed***@discussions.microsoft.com> wrote in message
news:AA**********************************@microsof t.com...
Hi Diego

Do you mean Client, or server side validation ?

kind regards

Ronnie

"Diego" wrote:
Hi all a quick question: how can I validate a date in asp.net (2.0) with
c#?
I didn't find a quick anwer.
Thanks, Diego.


Nov 17 '05 #4

P: n/a

This option isn't the correct way because if the date is not valid occurs an
exception an spend a lot of time.

You must verify if the date is valid, manually, in other words you have to
validate the format and you have to verify if the day is correct in the
respectly month and year.

I have the same problem as you and the exception way doesn't work well!

"Ronnie Edgar" <Ro*********@discussions.microsoft.com> wrote in message
news:92**********************************@microsof t.com...
Use a custom validator for client side validation. with a datetime
validation script as the validationscript.

and server side

something like

try
{
datetime.parse (myVariable);
}
catch (exception ex)
{
throw;
}

"Diego" wrote:
Possibly both :)
Thanks, Diego
"Ronnie Edgar" <Ronnie Ed***@discussions.microsoft.com> wrote in message
news:AA**********************************@microsof t.com...
> Hi Diego
>
> Do you mean Client, or server side validation ?
>
> kind regards
>
> Ronnie
>
> "Diego" wrote:
>
>> Hi all a quick question: how can I validate a date in asp.net (2.0)
>> with
>> c#?
>> I didn't find a quick anwer.
>> Thanks, Diego.
>>
>>
>>



Nov 17 '05 #5

P: n/a
KH
In .NET 2.0 you can use DateTime.TryParse() rather than having to handle the
exception.
"Rodrigo Ferreira" wrote:

This option isn't the correct way because if the date is not valid occurs an
exception an spend a lot of time.

You must verify if the date is valid, manually, in other words you have to
validate the format and you have to verify if the day is correct in the
respectly month and year.

I have the same problem as you and the exception way doesn't work well!

"Ronnie Edgar" <Ro*********@discussions.microsoft.com> wrote in message
news:92**********************************@microsof t.com...
Use a custom validator for client side validation. with a datetime
validation script as the validationscript.

and server side

something like

try
{
datetime.parse (myVariable);
}
catch (exception ex)
{
throw;
}

"Diego" wrote:
Possibly both :)
Thanks, Diego
"Ronnie Edgar" <Ronnie Ed***@discussions.microsoft.com> wrote in message
news:AA**********************************@microsof t.com...
> Hi Diego
>
> Do you mean Client, or server side validation ?
>
> kind regards
>
> Ronnie
>
> "Diego" wrote:
>
>> Hi all a quick question: how can I validate a date in asp.net (2.0)
>> with
>> c#?
>> I didn't find a quick anwer.
>> Thanks, Diego.
>>
>>
>>


Nov 17 '05 #6

P: n/a
Rodrigo Ferreira wrote:

This option isn't the correct way because if the date is not valid occurs an
exception an spend a lot of time.
Define "a lot of time".
I think it was Jon Skeet who did a test a while back in a thread and
threw 10,000 odd exceptions a second.
Exception blocks like the below do have their place, maybe not in this
(although it would do the job just fine) case but "a lot of time" is a
very subjective phrase.

To the OP.
There are numerous date validation regexes out there which should do the
job just fine.
Try <http://www.regexlib.com>
for a couple of examples.

HTH
JB

You must verify if the date is valid, manually, in other words you have to
validate the format and you have to verify if the day is correct in the
respectly month and year.

I have the same problem as you and the exception way doesn't work well!

"Ronnie Edgar" <Ro*********@discussions.microsoft.com> wrote in message
news:92**********************************@microsof t.com...
Use a custom validator for client side validation. with a datetime
validation script as the validationscript.

and server side

something like

try
{
datetime.parse (myVariable);
}
catch (exception ex)
{
throw;
}

"Diego" wrote:

Possibly both :)
Thanks, Diego
"Ronnie Edgar" <Ronnie Ed***@discussions.microsoft.com> wrote in message
news:AA**********************************@micro soft.com...

Hi Diego

Do you mean Client, or server side validation ?

kind regards

Ronnie

"Diego" wrote:
>Hi all a quick question: how can I validate a date in asp.net (2.0)
>with
>c#?
>I didn't find a quick anwer.
>Thanks, Diego.
>
>
>


Nov 17 '05 #7

P: n/a
John B <jb******@yahoo.com> wrote:
This option isn't the correct way because if the date is not valid occurs an
exception an spend a lot of time.
Define "a lot of time".
I think it was Jon Skeet who did a test a while back in a thread and
threw 10,000 odd exceptions a second.


Far more than that - about 90,000 I think.
Exception blocks like the below do have their place, maybe not in this
(although it would do the job just fine) case but "a lot of time" is a
very subjective phrase.


Indeed :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8

P: n/a
Rodrigo Ferreira <rj*********@gmail.com> wrote:
This option isn't the correct way because if the date is not valid occurs an
exception an spend a lot of time.
Nonsense - you spend in the order of about 100th of a millisecond on a
reasonable processor in release mode.
You must verify if the date is valid, manually, in other words you have to
validate the format and you have to verify if the day is correct in the
respectly month and year.

I have the same problem as you and the exception way doesn't work well!


Does that mean you've performed tests of this *in release mode* and
have performance statistics? I'd be fascinated to see those statistics
- do you have that many users that you'll be throwing out tens of
thousands of invalid dates every second?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #9

P: n/a
Ronnie Edgar <Ro*********@discussions.microsoft.com> wrote:
Use a custom validator for client side validation. with a datetime
validation script as the validationscript.

and server side

something like

try
{
datetime.parse (myVariable);
}
catch (exception ex)
{
throw;
}


There's no point in catching the exception if you're just going to
throw it again - just let it bubble up if that's the behaviour you
want. If you *don't* want to throw an exception, then catch it (but
preferrably only specific ones rather than just plain Exception).

If you don't want to throw an exception, then as of .NET 2.0, you can
use DateTime.TryParse, which would be preferrable to just catching the
exception. (The exception will be a slight performance penalty - not
nearly the bugbear that it's commonly made out to be, but using
TryParse avoids it without you having to write any custom validation
code.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #10

P: n/a
fair point

"Jon Skeet [C# MVP]" wrote:
try
{
datetime.parse (myVariable);
}
catch (exception ex)
{
throw;
}


There's no point in catching the exception if you're just going to
throw it again - just let it bubble up if that's the behaviour you
want. If you *don't* want to throw an exception, then catch it (but
preferrably only specific ones rather than just plain Exception).

If you don't want to throw an exception, then as of .NET 2.0, you can
use DateTime.TryParse, which would be preferrable to just catching the
exception. (The exception will be a slight performance penalty - not
nearly the bugbear that it's commonly made out to be, but using
TryParse avoids it without you having to write any custom validation
code.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #11

P: n/a
In message <MP************************@msnews.microsoft.com >, Jon Skeet
<?@pobox.com.invalid> writes
If you don't want to throw an exception, then as of .NET 2.0, you can
use DateTime.TryParse, which would be preferrable to just catching the
exception. (The exception will be a slight performance penalty - not
nearly the bugbear that it's commonly made out to be, but using
TryParse avoids it without you having to write any custom validation
code.)


Just out of interest, has anyone looked at the IL for TryParse?

--
Steve Walker
Nov 17 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.