Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
278 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 200 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

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…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

877 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