473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

validating a Date

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
10 1446
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: David Gray | last post by:
Greetings all, Quick newbie type question: I would like to be able to trap non-numerical data entered into a textbox via CTRL+C and/or Shift+Insert. I realise that this data can be...
6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
5
by: Astra | last post by:
Hi All I have a <SELECT> for the month (1 .. 12) and a <SELECT> for the year (2004 .... 2020), do you know of any js validation check I can use to check whether these values are older than...
5
by: Steve | last post by:
I am currently trying to validate data in an access database. I need to verify that columns containing date information are in the format ddmmyyyy and columns containg time information are in the...
1
by: panche | last post by:
I'm developing a fairly simple user control that has two textboxes for date/time entry (a from date/time and a to date/time). One of my requirements is that there should be no button that sets...
1
by: viki.sanjeeva | last post by:
Hi, There is a date field in JSP form where user will enter date in dd-mm-yyyy format. Now before saving into DB2, I want to validate the date format against dd-mm-yyyy format and then save into...
21
by: Darin | last post by:
I have a form w/ a textbox and Cancel button on it. I have a routine to handle textbox.validating, and I have the form setup so the Cancel button is the Cancel button. WHen the user clicks on...
1
by: lenin42001 | last post by:
Help i've just done a basic calculator with three textboxes a label & a button, basically when you add a number in textbox1 to the one in textbox2 it appears in textbox3 .........now the question...
1
by: =?Utf-8?B?Ym9iYnk=?= | last post by:
I have a textBox Where I type date. I want to validate it. Im using customValidate controls. I have this function in my code behind page void DatesValidate(object source,...
6
by: Richard | last post by:
I'm validating a date and time string which must be EXACTLY of the format yy-mm-dd hh:mm:ss and extracting the six numeric values using sscanf. I'm using the format string "%2u-%2u-%2u...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.