?
Solved

date validation

Posted on 2006-10-26
11
Medium Priority
?
216 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
Independent Software Vendors: 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

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!

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

764 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