Entering dates

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi all,

What is an elegant way of allowing a user to enter a date into a web form?
I'd like the date to be entered in dd/MM/yyyy format and validated on the
client as well.

Regards
John.
 
John said:
Hi all,

What is an elegant way of allowing a user to enter a date into a web form?
I'd like the date to be entered in dd/MM/yyyy format and validated on the
client as well.

Regards
John.

Use a regular expression validator with
"(0?[1-9]|[1-2][0-9]|30|31)/(1[0-2]|0?[1-9])/[0-9][0-9][0-9][0-9](.)*" as
the validation Expression (this however will accept 31/2/9999 as a valid
date. Feel free to tighten it up if you want). I believe it will be
validated clientside.

If you want to be really picky, write a custom validator in JScript (just
remember to do a stricter check on the server side as well as not all
browsers will run JScript).
 
Microsoft has provided the CompareValidator to check dates. Set its Operator
property to DataTypeCheck and Type property to Date.
It follows the current culture of the page to determine the
ShortDatePattern. For example, if you are in the UK, you can set <%@ Page
culture='en-UK' %>.

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

Brian Barnes said:
John said:
Hi all,

What is an elegant way of allowing a user to enter a date into a web
form?
I'd like the date to be entered in dd/MM/yyyy format and validated on the
client as well.

Regards
John.

Use a regular expression validator with
"(0?[1-9]|[1-2][0-9]|30|31)/(1[0-2]|0?[1-9])/[0-9][0-9][0-9][0-9](.)*" as
the validation Expression (this however will accept 31/2/9999 as a valid
date. Feel free to tighten it up if you want). I believe it will be
validated clientside.

If you want to be really picky, write a custom validator in JScript (just
remember to do a stricter check on the server side as well as not all
browsers will run JScript).
 
Back
Top