Solved

date validation

Posted on 2006-10-26
11
209 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:andieje
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I should mention this is .net 2.0 and TryParse is not available in 1.0 or 1.1
0
 

Author Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

9 Experts available now in Live!

Get 1:1 Help Now