andieje
asked on
date validation
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sorry
'added a Imports System.Text.RegularExpress ions 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
'added a Imports System.Text.RegularExpress
'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
Dim myMatch As Match
myMatch = myRegEx.Match(Contents)
Return myMatch.Success
End Function
where Contents is txtDate.Text
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
ASKER
new yuppie - i'm already using a date picker - the client wants a textbox instead. what can you do???? :)
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?...
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?...
Sorry if this was already recommended but try this:
Dim c As System.Globalization.Cultu reInfo = System.Globalization.Cultu reInfo.Cur rentCultur e
Dim dt As System.Globalization.DateT imeFormatI nfo = c.DateTimeFormat
Dim newdt As System.Globalization.DateT imeFormatI nfo = CType(dt.Clone, Globalization.DateTimeForm atInfo)
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.DateTimeStyl es.None, ParsedDate) Then
MsgBox(ParsedDate.ToShortD ateString)
Else
MsgBox("Failed")
End If
Try each test in turn to see if it works.
Dim c As System.Globalization.Cultu
Dim dt As System.Globalization.DateT
Dim newdt As System.Globalization.DateT
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.DateTimeStyl
MsgBox(ParsedDate.ToShortD
Else
MsgBox("Failed")
End If
Try each test in turn to see if it works.
I should mention this is .net 2.0 and TryParse is not available in 1.0 or 1.1
ASKER
Yes IdleMind - you're correct. I was just curious.
'added a Imports System.Text.RegularExpress
'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
Dim myMatch As Match
myMatch = myRegEx.Match(txtDate.Text
Return myMatch.Success
End Function