nclrwntr Posted February 28, 2003 Posted February 28, 2003 (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 February 28, 2003 by nclrwntr Quote
*Experts* Volte Posted February 28, 2003 *Experts* Posted February 28, 2003 You can use the IsDate function. If Not IsDate(fullDate) Then MessageBox.Show("Invalid Date!") End If Quote
nclrwntr Posted February 28, 2003 Author Posted February 28, 2003 (edited) lol. I forgot to mention that I'm prohibited from using that as well.:) And it doesn't seem to work. Edited February 28, 2003 by nclrwntr Quote
*Experts* Volte Posted February 28, 2003 *Experts* Posted February 28, 2003 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. :-\ Quote
nclrwntr Posted March 1, 2003 Author Posted March 1, 2003 Here, here. there are easier ways but we can't use them since we haven't covered it yet. blech! Quote
bpayne111 Posted March 1, 2003 Posted March 1, 2003 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 Quote i'm not lazy i'm just resting before i get tired.
bpayne111 Posted March 1, 2003 Posted March 1, 2003 OH by the way if you are subtracting dates by chance the DateDiff function may be of use to use to you Quote i'm not lazy i'm just resting before i get tired.
bpayne111 Posted March 1, 2003 Posted March 1, 2003 anytime Quote i'm not lazy i'm just resting before i get tired.
CaNaDiAn BaCoN Posted March 3, 2003 Posted March 3, 2003 This works, but... This expression should provide the result you want, if you're allowed to use it DateTime.IsLeapYear(CInt(year)) Quote
*Experts* Nerseus Posted March 3, 2003 *Experts* Posted March 3, 2003 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 Quote "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
nclrwntr Posted March 4, 2003 Author Posted March 4, 2003 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.:) Quote
*Experts* Nerseus Posted March 4, 2003 *Experts* Posted March 4, 2003 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 Quote "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
nclrwntr Posted March 4, 2003 Author Posted March 4, 2003 yup got that too. div by 4 but not if div by 100 but yes if div by 400 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.