date-time validation

C

Chris

Hi,

I have a texbox in a webform that is supposed to hold Dates in a specific
format :
e.g. 20-12-2004

What is the best way to make sure that the entered data is in the correct
format ?

Is there a possibillity to use a special kind of control for that purpose,
as in Winforms where one can use the DateTimePicker.

thnx
Chris
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi Chris,

You can use a RegularExpressionValidador or a customValidator for this.
Or you can do a "similar" thing that in a win. app , instead of
DateTimePicker you can create a popup ( or show a layer ) with an instance
of Calendar this improve the UI of the page too, then you update the TextBox
with the value selected, I'm sure that if you search a little you can find
an example with an explanation of how to do it, if not email me as I do that
in a couple of projects but I have not it in a compact easy to understand
way :).

Cheers,
 
P

Peter Blum

How about the "official" approach... The CompareValidator determines if
dates, integers, currencies and decimals are in a valid format and it
follows the CultureInfo object associated with the page to determine format.

Set the CompareValidator.Type=Date and Operator=DataTypeCheck.

A web form uses the culture associated with the current thread
(System.Threading.Thread.CurrentThread.CurrentCulture). So create the
CultureInfo object with its DateTimeFormat property using
ShortDatePattern="MM-dd-yyyy" and DateSeparator="-". You can do this either
at the beginning of Page_Load or in the Global.asax file in the
Application_BeginThread method (if this is a global setting).

FYI: If you are looking for a DateTextBox with popup calendar and supporting
validators that can support this date format, check out my "Peter's Date
Package" at http://www.peterblum.com/datecontrols/home.aspx.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,630
Members
45,338
Latest member
41Pearline46

Latest Threads

Top