473,770 Members | 6,506 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Checking for a valid date

What's the easiest way to verify the user had entered a valid date ?
Nov 21 '05 #1
28 2039
"romy" <ro******@Power up1.com> schrieb
What's the easiest way to verify the user had entered a valid date ?


Call Date.Parse in a Try/Catch block. If there's no excecption the date was
valid.
Armin

Nov 21 '05 #2


romy wrote:
What's the easiest way to verify the user had entered a valid date ?


The IsDate() function.

--
Larry Lard
Replies to group please

Nov 21 '05 #3
"romy" <ro******@Power up1.com> schrieb:
What's the easiest way to verify the user had entered a valid date ?


'Microsoft.Visu alBasic.Informa tion.IsDate'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #4
I recommend against this method in most cases; exceptions are
resource-intensive and shouldn't really be used for raw user-data
validation. It probably wouldn't matter much on a Win Forms app, but I
would really back away from this on ASP.NET if you're expecting any
volume of traffic.

The IsDate would be a better way to go.

Jason

www.pettysconsulting.com
Armin Zingler wrote:
"romy" <ro******@Power up1.com> schrieb
What's the easiest way to verify the user had entered a valid date ?


Call Date.Parse in a Try/Catch block. If there's no excecption the date
was valid.
Armin

Nov 21 '05 #5
Jason,

Doesn't IsDate use this same technique internally?

Kerry Moorman

"Jason Pettys" wrote:
I recommend against this method in most cases; exceptions are
resource-intensive and shouldn't really be used for raw user-data
validation. It probably wouldn't matter much on a Win Forms app, but I
would really back away from this on ASP.NET if you're expecting any
volume of traffic.

The IsDate would be a better way to go.

Jason

www.pettysconsulting.com
Armin Zingler wrote:
"romy" <ro******@Power up1.com> schrieb
What's the easiest way to verify the user had entered a valid date ?


Call Date.Parse in a Try/Catch block. If there's no excecption the date
was valid.
Armin

Nov 21 '05 #6
I'm not sure whether IsDate uses Exceptions as the primary mechanism.
Does anyone know for sure whether IsDate uses an exception for invalid
dates (1) always, (2) sometimes, or (3) never?

If I found out that it did I wouldn't use it; here's an msdn blog that
talks about it, the best quote being, "...pretend that the throw
statement makes the computer beep 3 times, and sleep for 2 seconds. If
you still want to throw under those circumstances, go for it."

http://blogs.msdn.com/ricom/archive/.../19/44697.aspx

The feedback from Jeremy Wilson on this next page seems to indicate that
IsDate does not (or at least doesn't use it alone):

http://blogs.crsw.com/mark/archive/2005/04/06/829.aspx

If I found out that IsDate DID rely primarily on an exception for
invalid values I would use something similar to that referenced in this
next article, the idea being to catch most invalid dates without
throwing an exception:

http://searchvb.techtarget.com/vsnet...293037,00.html

Jason

www.pettysconsulting.com
Kerry Moorman wrote:
Jason,

Doesn't IsDate use this same technique internally?

Kerry Moorman

"Jason Pettys" wrote:

I recommend against this method in most cases; exceptions are
resource-intensive and shouldn't really be used for raw user-data
validation. It probably wouldn't matter much on a Win Forms app, but I
would really back away from this on ASP.NET if you're expecting any
volume of traffic.

The IsDate would be a better way to go.

Jason

www.pettysconsulting.com
Armin Zingler wrote:
"romy" <ro******@Power up1.com> schrieb
What's the easiest way to verify the user had entered a valid date ?

Call Date.Parse in a Try/Catch block. If there's no excecption the date
was valid.
Armin

Nov 21 '05 #7
Armin,

Call Date.Parse in a Try/Catch block. If there's no excecption the date
was valid.

This is what internally iw done in IsDate

(You can check it by setting the stop on all throwed events in the debugger)

:-)

Cor
Nov 21 '05 #8
"Jason Pettys" <pe****@nospam. nospam> schrieb:
I'm not sure whether IsDate uses Exceptions as the primary mechanism. Does
anyone know for sure whether IsDate uses an exception for invalid dates
(1) always, (2) sometimes, or (3) never?


According to Lutz Roeder's Reflector, the method's implementation looks like
this:

\\\
Public Shared Function IsDate(ByVal Expression As Object) As Boolean
If (Not Expression Is Nothing) Then
If TypeOf Expression Is DateTime Then
Return True
End If
If TypeOf Expression Is String Then
Try
Dim time1 As DateTime =
DateType.FromSt ring(CType(Expr ession, String))
Return True
Catch exception1 As Exception
End Try
End If
End If
Return False
End Function
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #9
Jason,

You can catch the exception that IsDate throws internally like this:

From the Debug menu choose Exceptions. Select Common Language Runtime
Exceptions. Select Break into the debugger when the exception is thrown.

Now run some code that calls IsDate with an invalid date. You should get
this message:

"A first chance exception of type 'System.FormatE xception' occurred in
mscorlib.dll

Additional information: The string was not recognized as a valid DateTime.
There is a unknown word starting at index 0."

So I am assuming that IsDate throws an exception internally when it is sent
an invalid date. Then IsDate catches the exception and returns False.

I guess the real proof would be to look at the IL code.

Kerry Moorman
"Jason Pettys" wrote:
I'm not sure whether IsDate uses Exceptions as the primary mechanism.
Does anyone know for sure whether IsDate uses an exception for invalid
dates (1) always, (2) sometimes, or (3) never?

If I found out that it did I wouldn't use it; here's an msdn blog that
talks about it, the best quote being, "...pretend that the throw
statement makes the computer beep 3 times, and sleep for 2 seconds. If
you still want to throw under those circumstances, go for it."

http://blogs.msdn.com/ricom/archive/.../19/44697.aspx

The feedback from Jeremy Wilson on this next page seems to indicate that
IsDate does not (or at least doesn't use it alone):

http://blogs.crsw.com/mark/archive/2005/04/06/829.aspx

If I found out that IsDate DID rely primarily on an exception for
invalid values I would use something similar to that referenced in this
next article, the idea being to catch most invalid dates without
throwing an exception:

http://searchvb.techtarget.com/vsnet...293037,00.html

Jason

www.pettysconsulting.com
Kerry Moorman wrote:
Jason,

Doesn't IsDate use this same technique internally?

Kerry Moorman

"Jason Pettys" wrote:

I recommend against this method in most cases; exceptions are
resource-intensive and shouldn't really be used for raw user-data
validation. It probably wouldn't matter much on a Win Forms app, but I
would really back away from this on ASP.NET if you're expecting any
volume of traffic.

The IsDate would be a better way to go.

Jason

www.pettysconsulting.com
Armin Zingler wrote:

"romy" <ro******@Power up1.com> schrieb
>What's the easiest way to verify the user had entered a valid date ?
>
>

Call Date.Parse in a Try/Catch block. If there's no excecption the date
was valid.
Armin

Nov 21 '05 #10

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

Similar topics

8
2766
by: John V | last post by:
What kind of regular expression pattern is needed to check if URL is valid? It's enought if most of cases are covered. I have PHP 4.x. Br
7
7598
by: - ions | last post by:
I have created a JComboBox with its Items as a list of "M" numbers ie. M1,M2,M3.......throgh too M110 (thes are the messier objects, a catolouge of deep sky objects) the user selects of of these and views it aswell as infomation. The program also has a JTextFiels which allows the user to enter the M number. The problem i have is checking that what the user has entered is valid, that being an M followed by 1 - 110 Nothing else, i thought of...
67
4283
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. Unfortunately these ad hominem rhetorts are frequently introduced into purely technical discussions on the feasibility of supporting such functionality in C++. That usually serves to divert the discussion from the technical subject to a discussion of the...
4
4607
by: SQLScott | last post by:
I have created a VB.Net app that takes data from Visual Foxpro and inserts it into SQL Server. The problem I am running into is that I have come across records in Foxpro where the dates are as follows: '08/08/0997 12:00:00 PM' Obviously, SQL Server does not accept 0997 as a valid date, and the back of my mind is asking why is Foxpro even allowing this. But anyway, my question
11
33323
by: Bob Day | last post by:
The IsDate code below should result in False, instead it throws the exception below. Why? How do I check if a string can be converted to a date if this function does not work properly? Bob code: Dim blnDate_Valid As Boolean = True Dim x As String = "Hello"
4
2385
by: Patient Guy | last post by:
Does anyone have any coding rules they follow when doing argument checking? When arguments fail during check, do you return from the call with an ambiguous return value, or do you throw exceptions?
17
5284
by: Petyr David | last post by:
Just looking for the simplest. right now my perl script returns an error messge to the user if the date string is invalid. would like to do this before accessing the server. TX
7
14197
by: kannushree | last post by:
i have built a program ,that checks wheteher a date is valid or not. now if i want to add date,month year (in same input) to a particular date,,how wud the logic work? it should be like dis : void add(int d, int m,int y)
6
2138
by: Mike P | last post by:
How do I check for a valid datetime, where both the date and the time must be entered in the format DD/MM/YYYY HH:MM? Using Convert.ToDateTime would work even if a time is not added so I can't use this? *** Sent via Developersdex http://www.developersdex.com ***
0
9425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10228
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
10057
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
10002
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
9869
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
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3970
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
3575
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.