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

validating a Date

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
I use all CustomValidators personally (for many reasons) so I do it with a
simple Try/Catch of a Convert or .Parse call

--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"Chris" <ch********@pandora.be> wrote in message
news:BG**********************@phobos.telenet-ops.be...
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

Nov 18 '05 #2

P: n/a
Chris wrote:
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
Nov 18 '05 #3

P: n/a
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" <ch********@pandora.be> wrote in message
news:BG**********************@phobos.telenet-ops.be...
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

Nov 18 '05 #4

P: n/a
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.
"Chris" <ch********@pandora.be> wrote in message
news:BG**********************@phobos.telenet-ops.be...
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

Nov 18 '05 #5

P: n/a
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

"Scott Mitchell [MVP]" <mi******@4guysfromrolla.com> wrote in message
news:40**************@4guysfromrolla.com...
Chris wrote:
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

Nov 18 '05 #6

P: n/a
I mean to validate based on a regular expression.

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:OX**************@TK2MSFTNGP11.phx.gbl...
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.
"Chris" <ch********@pandora.be> wrote in message
news:BG**********************@phobos.telenet-ops.be...
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


Nov 18 '05 #7

P: n/a
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: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Steve Drake" <Steve@_NOSPAM_.Drakey.co.uk> wrote in message
news:Oo**************@TK2MSFTNGP10.phx.gbl...
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

"Scott Mitchell [MVP]" <mi******@4guysfromrolla.com> wrote in message
news:40**************@4guysfromrolla.com...
Chris wrote:
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


Nov 18 '05 #8

P: n/a
i just put this in my toolkit. i will shamelessly claim that i have been
using it for years :-)

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
"Peter Blum" <PL****@Blum.info> wrote in message
news:er****************@tk2msftngp13.phx.gbl...
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: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Steve Drake" <Steve@_NOSPAM_.Drakey.co.uk> wrote in message
news:Oo**************@TK2MSFTNGP10.phx.gbl...
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

"Scott Mitchell [MVP]" <mi******@4guysfromrolla.com> wrote in message
news:40**************@4guysfromrolla.com...
> Chris wrote:
> > 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



Nov 18 '05 #9

P: n/a
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
"Peter Blum" <PL****@Blum.info> wrote in message
news:er**************@tk2msftngp13.phx.gbl...
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: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Steve Drake" <Steve@_NOSPAM_.Drakey.co.uk> wrote in message
news:Oo**************@TK2MSFTNGP10.phx.gbl...
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

"Scott Mitchell [MVP]" <mi******@4guysfromrolla.com> wrote in message
news:40**************@4guysfromrolla.com...
Chris wrote:
> 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



Nov 18 '05 #10

P: n/a
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.CurrentUICul ture =
System.Globalization.CultureInfo.CreateSpecificCul ture("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: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Steve Drake" <Steve@_NOSPAM_.Drakey.co.uk> wrote in message
news:uA**************@TK2MSFTNGP09.phx.gbl...
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
"Peter Blum" <PL****@Blum.info> wrote in message
news:er**************@tk2msftngp13.phx.gbl...
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: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Steve Drake" <Steve@_NOSPAM_.Drakey.co.uk> wrote in message
news:Oo**************@TK2MSFTNGP10.phx.gbl...
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

"Scott Mitchell [MVP]" <mi******@4guysfromrolla.com> wrote in message
news:40**************@4guysfromrolla.com...
> Chris wrote:
> > 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



Nov 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.