Solved

check valid date in format  YYYY/MM/DD was entered: currently i user cdate(mystring) and if exception, then i prompt user

Posted on 2004-10-19
5
264 Views
Last Modified: 2008-02-01
hi, i have a form where the user has to enter date in format : YYYY/MM/DD
how this is the only way i know of for me to validate if a valid date was entered.
but it is horrible and wrong.
is there a better way to check if a valid user date was entered?
i cant get working regular expression for .net for a valid date.
i use vb.net , but posted this is .net section if there is possible a generic solution.

          GetMyDateInput = MyForm.TextBox1.Text
            Try
                Dim strCheck As Object = CDate(GetMyDateInput)
            Catch ex As Exception
                MessageBox.Show("Cannot Convert " & GetMyDateInput & " to Date")
               exit sub
            End Try

thanks!
0
Comment
Question by:jxharding
  • 2
  • 2
5 Comments
 
LVL 25

Expert Comment

by:apeter
ID: 12346142
Hi,
   Below is the link to check whether a given date is a valid date or not.

http://javascript.internet.com/calculators/date-difference.html

If u want to enfornce only YYYY/MM/DD, then you can 3 separate boxes one for year, Month and Date and u can easily validate those values also.

Cheers
Aaron
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 50 total points
ID: 12346162
if you know that the user has entered the value in the form YYYY/MM/DD then you can do this:

        Dim intDays() As Integer = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
        Dim Months() As String = {"January", "February", "March", "April", "May", "June", "July", _
                              "August", "September", "October", "November", "December"}
        Dim strInput As String = "2004/2/30"
        Dim intYear As Integer
        Dim intMonth As Integer
        Dim intDay As Integer
        Dim parts As String() = strInput.Split("/")
        Dim Valid As Boolean = True

intYear = parts(0)
intMonth = parts(1)
intDay = parts(2)
'validate the year - must be between 1998 and 2010
If intYear > 2010 Or intYear < 1998 Then
     MessageBox.Show("The Year you provided (" & intYear & ") is not Valid", vbOKOnly)
     Valid = False
'validate the Month (1 to 12)
ElseIf intMonth < 1 Or intMonth > 12 Then
     MessageBox.Show("The Month you provided (" & intMonth & ") is not valid", vbOKOnly)
     Valid = False
'Validate the day (> 0 and <= the number of days in the month (including Leap year check)
ElseIf intDay < 1 Or intDay > intDays(intMonth - 1) + IIf((intYear \ 4) * 4 = intYear, 1, 0) Then
     MessageBox.Show("the Day you provided (" & intDay & ") is not valid for " & Months(intMonth - 1) & " in " & intYear, vbOKOnly)
     Valid = False
End If
If Not Valid Then
    MessageBox.Show("The data you provided (" & strInput & ") was not valid", vbOKOnly)
Else
    ' this was a valid date
End If
   
AW
0
 

Author Comment

by:jxharding
ID: 12368137
hi, i dont know if user entered in format yyyy/mm/dd.
should i just check if the 5th & 8th chars are = ""?
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 12416131
you can do this:

dim iLoc as Integer = strInput.IndexOf("/")

If iLoc = 4 then  ' IndexOf starts with 0 for eh first character, so 4 means the '/' was after the 4th character
   ' string probably formatted as "yyyy/mm/dd"
Elseif iLoc = 2 then
   'string probably formatted as "mm/dd/yyyy"
end if

AW
0
 

Author Comment

by:jxharding
ID: 12420013
thanks a million arthur_wood!
this function will definitely be used in my projects!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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