Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

date validation

Posted on 2006-10-26
11
Medium Priority
?
217 Views
Last Modified: 2010-04-23
Hi

I'm sure this must be such a common task that I thought i'd ask on here rather than reinvent the wheel...

does anyone have a function that can see if a text string is a valid date. The date will be in the format

d/m/yy

or

d/m/yyyy

thanks
andrea
0
Comment
Question by:andieje
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 17

Accepted Solution

by:
ZeonFlash earned 800 total points
ID: 17812495
Try using the Date.Parse method (with globalization)

Dim dt As Date = Date.Parse("22/3/06", New System.Globalization.CultureInfo("de-DE"))
0
 
LVL 13

Assisted Solution

by:newyuppie
newyuppie earned 600 total points
ID: 17812524
may i suggest you place a DateTimePicker control instead of letting the user enter the date manually. you'll avoid a lot of headaches (learned the hard way)...

as to your question, im not sure if its appropiate way of doing it, but somewhere i read that you could check for example if
DateTime.Now.ToString(txtDate.Text) raises an exception: if it does, the string is not valid date form.

on the other hand im sure there must be some regular expression from which you could check date formats... ill look into it
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17812558
Date Format, checks for MM/DD/YYYY: "\d{1,2}\/\d{1,2}/\d{4}".



'added a Imports System.Text.RegularExpressions to the
    'top of the class to simplify the code
    Private Function IsValid(ByVal Contents As String) As Boolean
        Dim myRegEx As New Regex("\d{1,2}\/\d{1,2}/\d{4}")
        Dim myMatch As Match
        myMatch = myRegEx.Match(txtDate.Text)
        Return myMatch.Success
    End Function
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:newyuppie
ID: 17812564
sorry
'added a Imports System.Text.RegularExpressions to the
    'top of the class to simplify the code
    Private Function IsValid(ByVal Contents As String) As Boolean
        Dim myRegEx As New Regex("\d{1,2}\/\d{1,2}/\d{4}")
        Dim myMatch As Match
        myMatch = myRegEx.Match(Contents)
        Return myMatch.Success
    End Function

where Contents is txtDate.Text
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 600 total points
ID: 17812573
0
 

Author Comment

by:andieje
ID: 17816275
Hi

I was always told it was bad practice to rely on generating exceptions to detect invalid input. Presumably if you use some sort of date.parse method you are relying on a exception to tell you have a mistake?

I'm sure I am completely wrong but i am interested to know if you are relying on an exception and why this is ok.

thanks a lot
andrea
0
 

Author Comment

by:andieje
ID: 17816278
new yuppie - i'm already using a date picker - the client wants a textbox instead. what can you do???? :)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 17816345
There is nothing wrong with relying on an exception to tell you if something is wrong/right...

If you don't want to use DateTime.Parse() or DateTime.ParseExact() then you will need to use either regular string parsing or regular expressions as newyuppie proposes.

Be aware though that even though a string passes the RegEx pattern that does NOT mean it is a VALID DATE.  It doesn't address whether the values for day, month, year make any sense.

To do that without exceptions will be quite involved as you will basically have to reinvent the wheel to determine if the date is valid or not.

But that is exactly what you were trying to avoid in the first place right?...
0
 
LVL 5

Expert Comment

by:xersoft
ID: 17819140
Sorry if this was already recommended but try this:

        Dim c As System.Globalization.CultureInfo = System.Globalization.CultureInfo.CurrentCulture
        Dim dt As System.Globalization.DateTimeFormatInfo = c.DateTimeFormat
        Dim newdt As System.Globalization.DateTimeFormatInfo = CType(dt.Clone, Globalization.DateTimeFormatInfo)

        newdt.ShortDatePattern = "dd/MM/yyyy"


        Dim test As String = "13/4/81"
        Dim Test2 As String = "13/4/1981"
        Dim Test3 As String = "4/13/1981"

        Dim ParsedDate As DateTime
        If DateTime.TryParse(Test3, newdt, Globalization.DateTimeStyles.None, ParsedDate) Then
            MsgBox(ParsedDate.ToShortDateString)
        Else
            MsgBox("Failed")
        End If

Try each test in turn to see if it works.
0
 
LVL 5

Expert Comment

by:xersoft
ID: 17819178
I should mention this is .net 2.0 and TryParse is not available in 1.0 or 1.1
0
 

Author Comment

by:andieje
ID: 17820981
Yes IdleMind - you're correct. I was just curious.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question