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
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

729 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