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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

18 Experts available now in Live!

Get 1:1 Help Now