473,795 Members | 3,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 19182
Hi,

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

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

Dim dtnow As DateTime

If DateTime.TryPar se("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.Reg ularExpressions .Regex("^\d{1,2 }\/\d{1,2}\/\d{4}$")
If regDate.IsMatch (strDate) Then
dtNow = Date.Parse(strD ate)
Me.Text = dtNow.ToString
End If

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

Ken
---------------------
"YardDancer " <da*********@ho tmail.comwrote in message
news:Oi******** ******@TK2MSFTN GP06.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***@bellsout h.netwrote in message
news:ux******** ******@TK2MSFTN GP06.phx.gbl...
Hi,

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

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

Dim dtnow As DateTime

If DateTime.TryPar se("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.Reg ularExpressions .Regex("^\d{1,2 }\/\d{1,2}\/\d{4}$")
If regDate.IsMatch (strDate) Then
dtNow = Date.Parse(strD ate)
Me.Text = dtNow.ToString
End If

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

Ken
---------------------
"YardDancer " <da*********@ho tmail.comwrote in message
news:Oi******** ******@TK2MSFTN GP06.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.Globaliz ation.CultureIn fo("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**********@d iscussions.micr osoft.comwrote in message
news:BF******** *************** ***********@mic rosoft.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
2332
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 'email_address' field is blank, however. Why doesn't it catch empty 'message' field? <form method="POST" action= "<? $message = $_POST; $email_address = $_POST; if ( empty($message) || empty($email_address) )
6
1975
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
31845
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 current date. If not, I'd like to display the error message to ValidationSummary. It seems to make sense to me to use CompareValidator but the problem is put the current date into CompareValidator. So, I created a hidden text field in my aspx. ...
4
4877
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 event of the control and add this code
2
1172
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 find a control validator that did this. Can someone help? Thanks. p.s. I am using .Net 2.0 and VS2005. David
1
1771
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 $3,000 for the first year and up to an additional $3,000 for the second year. The main form tracks the dates the offers were made. And I have incorporated a subform to track the disbursements, whether for the first year allotment or second year...
4
6357
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 to see if all entries are uppercase
0
1040
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 validate each lines in the Text box against the standard xsd file for suppose it should not save any invalid entries in the xml file. Is there any way to validate the XML texts directly from the RichTextBox? If this is not possile atleast i want...
5
13849
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
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10436
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10213
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10163
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9040
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3722
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.