Solved

date validation

Posted on 2006-10-26
11
211 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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
 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

786 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