Completing Calendars with VBScript Date Functions - Validating user input
(Page 4 of 4 )
I promised that I would should you how to validate the user input. There are a number of ways to accomplish this, but I’ve chosen to use regular expressions because they are quite often used for validating user input. To do this, it’s best to create a separate function that retrieves the user’s input and validates it. This way, the function can be repeated if the user's input isn’t valid.
Replace this:
strDate = InputBox("Please enter a month and year as MM/YYYY.", "Microsoft Scripting Games 2008")
If strDate = "" Then WScript.Quit
With this:
strDate = GetValidDate()
The portion of the main script that retrieves user input should be replaced. We’ll be handling this within our new function instead.
Function GetValidDate
strDate = InputBox("Please enter a month and year as MM/YYYY.", "Microsoft Scripting Games 2008")
If strDate = "" Then WScript.Quit
GetValidDate = strDate
Now our function should start out by receiving the user’s input. We immediately assign this value as our function’s return value. Since our function will only loop if the data is not valid, we want to set an initial return value for our function so that it doesn’t return empty if all is well. We can change the return value later in our function if necessary.
Set re = New RegExp
re.IgnoreCase = True
re.Pattern = "^(dd?)/(dddd?)$"
If re.Test(strDate) Then
Set colMatches = re.Execute(strDate)
intMonth = CInt(colMatches(0).SubMatches(0))
intYear = CInt(colMatches(0).SubMatches(1))
If intMonth < 1 Or intMonth > 12 Or intYear < 100 Then
WScript.Echo "You have not entered a valid date. Please try again."
GetValidDate = GetValidDate()
End If
Else
WScript.Echo "Your entry was not valid. Please try again."
GetValidDate = GetValidDate()
End If
End Function
The rest of the function looks like this. It uses a regular expression to check the validity of the user’s string. Without going into too much detail about regular expressions, let me see if I can break this down a little bit for you. Let’s take it in parts.
Set re = New RegExp
re.IgnoreCase = True
re.Pattern = "^(dd?)/(dddd?)$"
These lines create a new regular expression object and initialize some basic properties. The most important one here is the Pattern property. This determines what the regular expression considers valid. Patterns are far beyond the scope of this article. This particular pattern checks that the whole user’s string contains either one or two digits immediately followed by a forward slash, which is in turn followed by either three or four more digits. In other words, it allows a one or two digit month and a three or four digit year.
If re.Test(strDate) Then
Set colMatches = re.Execute(strDate)
intMonth = CInt(colMatches(0).SubMatches(0))
intYear = CInt(colMatches(0).SubMatches(1))
Next we have this code. We use the regular expression’s Test method to determine if the user input matches our pattern. If so, we retrieve the month and year values.
If intMonth < 1 Or intMonth > 12 Or intYear < 100 Then
WScript.Echo "You have not entered a valid date. Please try again."
GetValidDate = GetValidDate()
End If
Else
WScript.Echo "Your entry was not valid. Please try again."
GetValidDate = GetValidDate()
End If
Finally, we test those values to make sure that they are in the acceptable range. We’re simply telling the function to fail if the month is less than 1 or greater than 12, or if the year is less than 100 (VBScript Dates are supported from 100 AD). This allows the full range of supported Dates in VBScript. In the event that any of the parts are not valid, we notify the user and then set the function’s return value equal to the function itself. This causes a looping action, forcing the validation process to begin again.
And there you have it. A fully-operational calendar-building script that will work for any month in VBScript’s acceptable Date range, which is January 100 AD through December 9999 AD. Now you’re ready to put it to the test.
So on what day of the week was Napoleon born? Well, he was born August 15, 1769.
August 1769
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
According to our trusty script, that was a Tuesday. Until next time, keep coding!
You can download the full code for this article here.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |