473,387 Members | 1,863 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,387 software developers and data experts.

How to validate Date entries.

Hi All,

Does anybody have a routine for validating a date or DateTime entry in a
textbox.

I know i can use one of the DateTime.Parse(textbox.text) methods which
would throw a Fromat Exception.

What i am after is something like a IsNumeric() fuction or way i can test a
value if it is dateTime value and recieve a boolean answer i.e. true or
false.

Yard Dancer
Practicing at home before I attempt to dance at the hall.
Jul 30 '06 #1
5 19163
Hi,

If you are using vb 2005 I would use DateTime.TryParse otherwise
I would use a regex.

http://msdn2.microsoft.com/en-us/lib....tryparse.aspx

Dim dtnow As DateTime

If DateTime.TryParse("1/1/2006", dtnow) Then
Me.Text = dtnow.ToString
End If

Regex example

Dim dtNow As DateTime
Dim strDate As String = "1/1/2006"
Dim regDate As New
System.Text.RegularExpressions.Regex("^\d{1,2}\/\d{1,2}\/\d{4}$")
If regDate.IsMatch(strDate) Then
dtNow = Date.Parse(strDate)
Me.Text = dtNow.ToString
End If

http://www.regexlib.com/Search.aspx?k=date

Ken
---------------------
"YardDancer" <da*********@hotmail.comwrote in message
news:Oi**************@TK2MSFTNGP06.phx.gbl...
Hi All,

Does anybody have a routine for validating a date or DateTime entry in a
textbox.

I know i can use one of the DateTime.Parse(textbox.text) methods which
would throw a Fromat Exception.

What i am after is something like a IsNumeric() fuction or way i can test
a value if it is dateTime value and recieve a boolean answer i.e. true or
false.

Yard Dancer
Practicing at home before I attempt to dance at the hall.

Jul 30 '06 #2
I am Programming with VB.NET 2003

--
Yard Dancer
Practicing at home before I attempt to dance at the hall.
"Ken Tucker [MVP]" <vb***@bellsouth.netwrote in message
news:ux**************@TK2MSFTNGP06.phx.gbl...
Hi,

If you are using vb 2005 I would use DateTime.TryParse
otherwise I would use a regex.

http://msdn2.microsoft.com/en-us/lib....tryparse.aspx

Dim dtnow As DateTime

If DateTime.TryParse("1/1/2006", dtnow) Then
Me.Text = dtnow.ToString
End If

Regex example

Dim dtNow As DateTime
Dim strDate As String = "1/1/2006"
Dim regDate As New
System.Text.RegularExpressions.Regex("^\d{1,2}\/\d{1,2}\/\d{4}$")
If regDate.IsMatch(strDate) Then
dtNow = Date.Parse(strDate)
Me.Text = dtNow.ToString
End If

http://www.regexlib.com/Search.aspx?k=date

Ken
---------------------
"YardDancer" <da*********@hotmail.comwrote in message
news:Oi**************@TK2MSFTNGP06.phx.gbl...
>Hi All,

Does anybody have a routine for validating a date or DateTime entry in a
textbox.

I know i can use one of the DateTime.Parse(textbox.text) methods which
would throw a Fromat Exception.

What i am after is something like a IsNumeric() fuction or way i can
test a value if it is dateTime value and recieve a boolean answer i.e.
true or false.

Yard Dancer
Practicing at home before I attempt to dance at the hall.


Jul 30 '06 #3
YardDancer,

What about VB's IsDate function?

Kerry Moorman

"YardDancer" wrote:
Hi All,

Does anybody have a routine for validating a date or DateTime entry in a
textbox.

I know i can use one of the DateTime.Parse(textbox.text) methods which
would throw a Fromat Exception.

What i am after is something like a IsNumeric() fuction or way i can test a
value if it is dateTime value and recieve a boolean answer i.e. true or
false.

Yard Dancer
Practicing at home before I attempt to dance at the hall.
Jul 30 '06 #4
It's tricky to write such a validation routine because "invalid date" is
quite relative.

For instance "13/1/2006" is a valid in some regions but not in others.

If you know the region you can do this:

string UK="13/1/2006";
DateTime UKDate = DateTime.Parse(UK,new System.Globalization.CultureInfo("en-gb"));

You could probably put this in a function and catch any format exceptions
and return false.

Hi All,

Does anybody have a routine for validating a date or DateTime entry
in a textbox.

I know i can use one of the DateTime.Parse(textbox.text) methods
which would throw a Fromat Exception.

What i am after is something like a IsNumeric() fuction or way i can
test a value if it is dateTime value and recieve a boolean answer i.e.
true or false.

Yard Dancer
Practicing at home before I attempt to dance at the hall.

Jul 30 '06 #5
Hi Kerry,

I want to write all my routines using methods and properties that are
consistent
with the .Net Framework class library. I am trying to avoid using
functions
from the VB run-time library.

--
Yard Dancer
Practicing at home before I attempt to dance at the hall.
Kerry Moorman" <Ke**********@discussions.microsoft.comwrote in message
news:BF**********************************@microsof t.com...
YardDancer,

What about VB's IsDate function?

Kerry Moorman

"YardDancer" wrote:
>Hi All,

Does anybody have a routine for validating a date or DateTime entry in a
textbox.

I know i can use one of the DateTime.Parse(textbox.text) methods which
would throw a Fromat Exception.

What i am after is something like a IsNumeric() fuction or way i can
test a
value if it is dateTime value and recieve a boolean answer i.e. true or
false.

Yard Dancer
Practicing at home before I attempt to dance at the hall.


Jul 30 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: deko | last post by:
I have a basic Feedback form - I want to prevent blank entries. The problem with the below code is that the form still Posts if the 'message' field is blank. The form will not post if the...
6
by: RSB | last post by:
Hi Every one Need some help to Validate the Date i have in the Form. ALso how to Convert the Date Data i read from table to yyyy/MMM/dd format. Thanks RSB
7
by: James P. | last post by:
Hello there, In my asp.net page using VB, I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is greater than or equal to the...
4
by: Michel Posseth [MCP] | last post by:
I have a problem with the date time picker validate event wich i believe is a bug How to reproduce : throw on a form a date time picker control and a textbox control select the validating...
2
by: David C | last post by:
I have a DropDownList control that has a value of 0 for not selected and a -1 for adding a new company. I want to be able to validate to say that this selected value is not 0 or -1 but I couldn't...
1
by: kickergirl | last post by:
I'm not sure my title actually describes my problem, but here it goes. I am creating a form to track account information for participants. Basically, a single participant can be offered up to...
4
by: Angela | last post by:
Hello How would one auto validate using the 'onchange' method a text box for invalid entries? Invalid entries would include: 1. Input string of '....' 2. Input string of 'aaaa' 2. Check...
0
by: anupam roy | last post by:
Hi, I have created my own XML text editor and using XmlReader to read the xml files I have opened one xml file for editing in the text box editor( i have . Now before saving the file, i want to...
5
by: shapper | last post by:
Hello, What is the Regex expression to validate a date time format as follows: dd-mm-yyyy hh:mm:ss An example: 20-10-2008 10:32:45
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.