|
-
Apr 30th, 2013, 12:29 PM
#1
Thread Starter
Junior Member
Date Must Be Between 1900 and 2100
Okay so basically my program is a program that validates dates in this format:
DD/MM/YYYY
However, I need I guess some sort of statement (my brain is thinking an IF ELSE) to make sure that the user does not enter a date that is less than 1900 and not greater than 2100; or in other words: it must be between 1900 and 2100.
I don't suppose anyone could help me? The code I have done so far is here:
Code:
Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
Dim dateString, format As String
Dim result As Date
Dim IsValid As Boolean = False
dateString = txtDate.Text
format = "dd/M/yyyy"
If format < Then
IsValid = False
End If
Try
result = Date.ParseExact(dateString, format, System.Globalization.CultureInfo.InvariantCulture)
IsValid = True
Catch
IsValid = False
End Try
If (IsValid) Then
MessageBox.Show("Valid.")
Else
MessageBox.Show("Invalid.")
End If
End Sub
The validation works but the assurance of the year doesn't work.
Thanks.
-
Apr 30th, 2013, 12:46 PM
#2
Re: Date Must Be Between 1900 and 2100
Start by proofreading:
format = "dd/M/yyyy"
If format < Then
Only one digit for Month? And if format is less than what? Also, format is a string, how can it be less than anything?
-
Apr 30th, 2013, 01:48 PM
#3
Re: Date Must Be Between 1900 and 2100
Use a DateTimePicker! You can set the min and max dates and it doesn't need any validation, making your entire code here redundant!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Apr 30th, 2013, 02:32 PM
#4
Thread Starter
Junior Member
Re: Date Must Be Between 1900 and 2100
It's a school thing that I have to do. I can't use a DateTimePicker.
@circuits2: Okay so how can I do it?
-
Apr 30th, 2013, 03:41 PM
#5
Re: Date Must Be Between 1900 and 2100
Some ideas
Code:
Dim dtMin As DateTime = #1/1/1900#
Dim dtMax As DateTime = #12/31/2100#
Dim myDate As DateTime = DateTime.Now
If myDate >= dtMin AndAlso myDate <= dtMax Then
'valid date
Else
'invalid date
End If
-
Apr 30th, 2013, 03:48 PM
#6
Thread Starter
Junior Member
Re: Date Must Be Between 1900 and 2100
I think you're on the right tracks, but basically I need to check the date that was entered in by the user.
-
Apr 30th, 2013, 03:49 PM
#7
Re: Date Must Be Between 1900 and 2100
It's a school thing that I have to do.
Ah, right. Should have led with that. Then we'd know to expect you to have to do everything the most difficult and inefficient way possible.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Apr 30th, 2013, 03:53 PM
#8
Thread Starter
Junior Member
Re: Date Must Be Between 1900 and 2100
Heh, yes, there are better ways. .
-
Apr 30th, 2013, 04:10 PM
#9
Re: Date Must Be Between 1900 and 2100
 Originally Posted by D.E.L.B.
I think you're on the right tracks, but basically I need to check the date that was entered in by the user.
See http://msdn.microsoft.com/en-us/libr....tryparse.aspx
-
Apr 30th, 2013, 04:17 PM
#10
Re: Date Must Be Between 1900 and 2100
you know that once you coerce the value into a date variable... it has a .Year property... and you can see if
myDateVar.Year >= 1900 andalso myDateVar.Year <= 2100
...
-tg
-
Apr 30th, 2013, 05:35 PM
#11
Thread Starter
Junior Member
Re: Date Must Be Between 1900 and 2100
http://puu.sh/2Km3U.jpg
This is what my window looks like. So basically the user enters the text into the textbox (it's named txtDate) and then I press the button and it validates the format of the date and if it's between 1900 and 2100.
Code:
If result.Year >= dtMin AndAlso result.Year <= dtMax Then
IsValid = True
Else
IsValid = False
End If
I tried this here but it doesn't work either.
@circuits2: I put just the one M because this type of date is valid:
13/05/1990
As well as this:
13/5/1990
@dbasnett: If you could explain to me how I can implement that in my code it would be greatly appreciated.
-
Apr 30th, 2013, 06:54 PM
#12
Re: Date Must Be Between 1900 and 2100
Show us what you have tried using TryParse or TryParseExact
-
May 1st, 2013, 07:04 AM
#13
Re: Date Must Be Between 1900 and 2100
Code:
If result.Year >= dtMin AndAlso result.Year <= dtMax Then
That's not what I said was it?
what I said was this:
Code:
myDateVar.Year >= 1900 andalso myDateVar.Year <= 2100
are you trying to compare the YEARS or the FULL DATE? your first post indicated that after checking to see if it's a valid date, then you need to check that the date is between 1900 and 2100 ... which are years... not dates...
-tg
-
May 1st, 2013, 11:45 AM
#14
Re: Date Must Be Between 1900 and 2100
 Originally Posted by techgnome
Code:
If result.Year >= dtMin AndAlso result.Year <= dtMax Then
That's not what I said was it?
what I said was this:
Code:
myDateVar.Year >= 1900 andalso myDateVar.Year <= 2100
are you trying to compare the YEARS or the FULL DATE? your first post indicated that after checking to see if it's a valid date, then you need to check that the date is between 1900 and 2100 ... which are years... not dates...
-tg
He could be labeling dtMin and dtMax as the year values perhaps? He wouldn't be able to compile it otherwise.
-
May 1st, 2013, 12:49 PM
#15
Re: Date Must Be Between 1900 and 2100
possibly, but then he stated this "I tried this here but it doesn't work either." ... which doesn't really tell me much either... for all I know it means his fingers fell off...
-tg
-
May 1st, 2013, 02:55 PM
#16
Re: Date Must Be Between 1900 and 2100
 Originally Posted by D.E.L.B.
http://puu.sh/2Km3U.jpg
This is what my window looks like. So basically the user enters the text into the textbox (it's named txtDate) and then I press the button and it validates the format of the date and if it's between 1900 and 2100.
Code:
If result.Year >= dtMin AndAlso result.Year <= dtMax Then
IsValid = True
Else
IsValid = False
End If
I tried this here but it doesn't work either.
@circuits2: I put just the one M because this type of date is valid:
13/05/1990
As well as this:
13/5/1990
@dbasnett: If you could explain to me how I can implement that in my code it would be greatly appreciated.
Assuming result is a datetime and dtMin and dtMax are what I posted previously:
Code:
If result >= dtMin AndAlso result <= dtMax Then
IsValid = True
Else
IsValid = False
End If
-
May 1st, 2013, 04:40 PM
#17
Thread Starter
Junior Member
Re: Date Must Be Between 1900 and 2100
 Originally Posted by techgnome
Code:
If result.Year >= dtMin AndAlso result.Year <= dtMax Then
That's not what I said was it?
what I said was this:
Code:
myDateVar.Year >= 1900 andalso myDateVar.Year <= 2100
are you trying to compare the YEARS or the FULL DATE? your first post indicated that after checking to see if it's a valid date, then you need to check that the date is between 1900 and 2100 ... which are years... not dates...
-tg
I am trying to compare the YEARS, sorry.
-
May 2nd, 2013, 09:37 AM
#18
Re: Date Must Be Between 1900 and 2100
 Originally Posted by D.E.L.B.
I am trying to compare the YEARS, sorry.
Then...what's the problem? There are two (maybe three, I haven't counted the entire thread) different posts that show you exactly what to do.
-
May 3rd, 2013, 03:46 AM
#19
Thread Starter
Junior Member
Re: Date Must Be Between 1900 and 2100
I'm not messing you guys about but I'm overwhelmed with the amount of solutions to this problem. I haven't use DateTime.TryParse and I have no idea how to implement it. I have looked at MS' documentation but it doesn't make sense to me.
-
May 3rd, 2013, 04:29 AM
#20
Re: Date Must Be Between 1900 and 2100
Step 1. try to convert the string into a date using DateTime.TryParse
Code:
Dim DateValue as Date
If Date.TryParse (TextBoxUserInputtedDate.Text , DateValue) Then
'Good date
Else
MessageBox.Show ("Invalid date entered")
End If
Step 2. Now you have it as a date variable, check it is within the correct year range as per #10 Techgnome
Code:
Dim DateValue as Date
If Date.TryParse (TextBoxUserInputtedDate,DateValue) Then
If DateValue.Year >= 1900 andalso DateValue.Year <= 2100 Then
'This is within the range so accept the input as correct
Else
MessageBox.Show ("Valid date entered but it is outside the required range")
End If
Else
MessageBox.Show ("Invalid date entered")
End If
-
May 6th, 2013, 12:22 PM
#21
Thread Starter
Junior Member
Re: Date Must Be Between 1900 and 2100
-
May 6th, 2013, 02:14 PM
#22
Re: Date Must Be Between 1900 and 2100
I should have explained this in the last post too since you said you didn't understand the MSDN documentation. I hope my explanation makes it clearer.
Code:
If Date.TryParse (TextBoxUserInputtedDate.Text , DateValue) Then
TryParse tries to convert from one datatype to another. In the above example, you want to convert the string TextBoxUserInputtedDate.Text to a date.
If it is a valid date it sets the second variable in the argument list (DateValue) to that date.
If it is not a valid date, it returns False, therefore allowing us to do
Code:
If Date.TryParse (...) = True Then
-
May 6th, 2013, 03:55 PM
#23
Thread Starter
Junior Member
Re: Date Must Be Between 1900 and 2100
Thank you, explained very clearly!
I just noticed something however, my DateTime has to be in the format of DD/MM/YYYY , the year formatting is correct though. I don't suppose you or anyone else could help me, please?
-
May 6th, 2013, 04:08 PM
#24
Re: Date Must Be Between 1900 and 2100
You should really try and understand MSDN's documentation. However, I'll give you a hand with these two functions.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|