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.
I didn't find a quick anwer.
Thanks, Diego.
Ronnie Edgar said:Hi Diego
Do you mean Client, or server side validation ?
kind regards
Ronnie
Define "a lot of time".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.
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.
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.
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.
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 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;
}
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.)