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
261 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
help with regular expression to search string 6 29
Store results in vb.net 3 24
Iteration Help (Asp.net VB) 5 24
Amazon S3 Images with .Net 3 10
Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

789 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