Solved

date validation

Posted on 2006-10-26
11
210 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
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 17

Accepted Solution

by:
ZeonFlash earned 200 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 150 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
 
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 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 150 total points
ID: 17812573
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 85

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
A short film showing how OnPage and Connectwise integration works.

947 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now