validating a Date

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Hi,

how do I validate a date using a RegularExpression-validator control ?

e.g. 05/20/2004
(May 20, 2004)

Or should I use another validator-control ? and how ?

Thanks

Chris
 
I use all CustomValidators personally (for many reasons) so I do it with a
simple Try/Catch of a Convert or .Parse call
 
You can use the RegularExpressionValidator, see code at the end.

As curt says, you can use try and catch but trying and catching is very
expesive as it uses lots of CPU time.

I ran Parse 5000 times and passed an invalid date, it took about 30 secons,
I ran it 5000 times on an valid date and it took less than 1 second. I
always run my system with Catch ALL handled errors to find any problems with
people using catch instend of checking for object being null ect.

(Code, this is SNIPS from a project that i am working on, so bits maybe
missing)

RegularExpressionValidator dateValidator;

dateValidator = new RegularExpressionValidator();
dateValidator.ControlToValidate = dateBox.ID;
dateValidator.Display = ValidatorDisplay.Dynamic;


dateValidator.ValidationExpression =
"^(?:(?:0?[1-9]|1[0-2])(\\/|-)(?:0?[1-9]|1\\d|2[0-8]))(\\/|-)(?:[1-9]\\d\\d\
\d|\\d[1-9]\\d\\d|\\d\\d[1-9]\\d|\\d\\d\\d[1-9])$|^(?:(?:0?[13578]|1[02])(?:
31(\\/|-))|(?:(?:0?[1,3-9]|1[0-2])(\\/|-)(?:29|30)))(\\/|-)(?:[1-9]\\d\\d\\d
|\\d[1-9]\\d\\d|\\d\\d[1-9]\\d|\\d\\d\\d[1-9])$|^(0?2(\\/|-)29)(\\/|-)(?:(?:
0[48]00|[13579][26]00|[2468][048]00)|(?:\\d\\d)?(?:0[48]|[2468][048]|[13579]
[26]))$ ";

dateValidator.ErrorMessage = "MM/DD/YYYY";
 
Chris said:
how do I validate a date using a RegularExpression-validator control ?

e.g. 05/20/2004
(May 20, 2004)

Or should I use another validator-control ? and how ?

Chris, use a CompareValidator. Set the Type to Date and the Operator to
DataTypeCheck.

hth
 
It may not be worth the effort to validate a date because of the complexity
that the algorithm would need to be to be robust.

1) You may want to allow the user to enter in dates in any valid format.
2) Different regions of the world have different date formats and
delimiters.
3) Different months have different days.
4) Have to take into account leap years.
5) And other issues that I cannot think of off the top of my head.

You may want to perform the validation on the server by using the
Convert.Parse method.
 
I never new you could use the CompareValidator todo this, I will re jig my
code.

Does it support multi region? eg uses the CLIENTS settings not the servers.

Steve
 
The CompareValidator is culture sensitive. It will validate the date in the
format of the current thread's CultureInfo. All Validators with a Type
property support CultureInfo on these types: Date, Integer, Decimal, and
Currency.

It's a shame how many people miss the CompareValidator and try to use a
regular expression. How much time must be wasted by the ASP.NET community!

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 
i just put this in my toolkit. i will shamelessly claim that i have been
using it for years :-)
 
When you say it uses the culture of the current thread is this the culture
that ASP.NET is running as, e.g. not the culture of the e.g. use the header
Accept-Language header, the system I am working on needs to be multi
language, e.g. present the page in the users chosen language including date
format and screen prompts, at present we have a big switch statement to set
the regular expression to validate the date.

Steve
 
ASP.NET is designed to let you assign the Culture you want to to the current
thread. All your page needs to know is which culture to use. Then you create
a CultureInfo object in Page_Load or Global.asax's Application_BeginRequest
like this:
System.Threading.Thread.CurrentThread.CurrentUICulture =
System.Globalization.CultureInfo.CreateSpecificCulture("id-ID");

Just be sure to do this before you use the validators (Page_Load is usually
early enough).

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 
Back
Top