Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

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:

 

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.

Edited by nclrwntr
  • *Experts*
Posted

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. :-\

Posted

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

i'm not lazy i'm just resting before i get tired.
  • *Experts*
Posted

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

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.:)

  • *Experts*
Posted

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...