Validating dates

J

John

Hi

I have a web control text box on my webform which I want to use for entering
dates. Is there a way to validate this text box field for valid dates? May
be using a validators such as regular expression validator?

Thanks

Regards
 
G

Guest

Use a CompareValidator control, it was designed for.

<asp:comparevalidator id="comparevalidator1" runat="server"
controltovalidate="TextBox1" operator="DataTypeCheck"
type="Date">*</asp:comparevalidator>
 
P

Peter Blum

CompareValidator can easily handle the request. Regular expressions are
complicated and have limitations like the inability to recognize a legal
date value.

The CompareValidator uses the current thread's CultureInfo
(System.Threading.Thread.CurrentThread.CurrentUICulture) to determine the
date format.
You can set the culture with your desired date format in <@ Page culture= >
or on CurrentUICulture in Page_Load(). See the .net docs on the CultureInfo
class to learn how to create them and the culture IDs for your culture.
You can also modify a cultureInfo to set the desired format:
CultureInfo.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy"
CultureInfo.DateTimeFormat.DateSeparator = "/"

Hint: You cannot modify the CultureInfo object assigned by default to
CurrentUICulture. Clone it or create a new instance. These you can modify.

--- 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,780
Messages
2,569,611
Members
45,279
Latest member
LaRoseDermaBottle

Latest Threads

Top