Validating a date in a compound IF statement

nclrwntr

Newcomer
Joined
Feb 28, 2003
Messages
6
I need to validate a date entered into a textbox in the form mm/dd/yy by using a massive compound IF statement. If the date is valid, nothing happens but if an invalid date is entered, a message box pops up saying, "invalid date."

this is what I have so far:

Visual Basic:
fulldate= txtdate.text
month = fulldate.Substring(0, 2)
day = fulldate.Substring(3, 2)
year = fulldate.Substring(6, 2)

If (month < 1 Or month > 12) Or (day < 1 Or day > 31) Or (year > 3 And year < 38) Or (month = 4 And day > 30) Or (month = 6 And day > 30) Or (month = 9 And day > 30) Or (month = 11 And day > 30) Then
            MsgBox("Invalid Date Entered", , "Data Entry Error")
            Exit Sub
        End If

I've tried a lot of different things to do the leapyear and non leapyear calculations but nothing works.

I can make it work using mod operators but we can't use them.
 
Last edited:
You can use the IsDate function.
Visual Basic:
If Not IsDate(fullDate) Then
  MessageBox.Show("Invalid Date!")
End If
 
lol. I forgot to mention that I'm prohibited from using that as well.:)

And it doesn't seem to work.
 
Last edited:
In that case, you will need to do some work. Date parsing is a very
complicated thing, in that you can have many different formats,
orders, styles, delimiters and not to mention leap years...

In my eyes, this exercise is needlessly complicated, but then again
I'm not the instructor. :-\
 
Not that this is totally relevant to your question but...
you should try the OrElse statement instead of Or
The OrElse statement will return true with the first expression that is true.
The Or statement on the other hand will evaluate all of the expressions regaurdless of thier value taking more time than the previous example.
Sometimes Or is the correct logic operator to use if you for instance wanted to evaluate a function reguardless of it's return value. In this case the OrElse statement may terminate before the Function is evaluated.

I taught my teacher that one... hehehehe
I can type you up some sample code if you like
 
OH by the way if you are subtracting dates by chance the DateDiff function may be of use to use to you
 
I assume you can't use DateTime.Parse(...) either?

I believe the "IsLeapYear" function checks the following:
if(year is evenly divisible by 4 and NOT 0, 1000, 3000, 5000, etc.)
leap year!
else
not leap year!

Since this seems to be an excercise, I'm not providing the details just the info :)

-Nerseus
 
Thanks for the input all!:)

I've decided to go with "year mod 4 = 0" style.

Bottome line is that it works right? He should accept this. I hope........

The only reason why he wouldn't is because we haven't learned it yet in class but I figured it out on my own. That counts for something.:)
 
Don't forget that every other 1000 years is NOT a leap year though it is evenly divisible by 4. So 1000 mod 4 = 0 but 1000 is not a leap year (2000 is, 3000 isn't, etc.)

-Nerseus
 
Back
Top