Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - Completing Calendars with VBScript Date Fu...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Completing Calendars with VBScript Date Functions
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-02-02

    Table of Contents:
  • Completing Calendars with VBScript Date Functions
  • Building the array
  • Completing the code
  • Validating user input

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

     

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek