Date validation

  • Thread starter Thread starter Diego
  • Start date Start date
D

Diego

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.
 
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;
}
 
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!
 
In .NET 2.0 you can use DateTime.TryParse() rather than having to handle the
exception.
 
Rodrigo said:
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
 
John B said:
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 :)
 
Rodrigo Ferreira said:
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?
 
Ronnie Edgar said:
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.)
 
fair point

Jon Skeet said:
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 said:
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?
 
Back
Top