How to Check for a Valid Date in Perl

The Perl programming language lets you create processing pages for forms and dynamic data to store on your Web server. When a user types in a date, it's important to ensure that the date is valid. The Perl "regular expression" function gives you the ability to check for valid dates. The code is long, but you can copy and paste the function into your Perl pages to incorporate the date-check system.

Instructions

    • 1

      Right-click the "PL" (Perl) file you want to edit and select "Open With." Click the Perl editor listed in the sub-menu to load the editor and code simultaneously.

    • 2

      Create a date variable if you do not already have one defined. The following code creates a date that is invalid, so it trips the function to respond with an "invalid date" error:

      $date '5/1/040';

    • 3

      Incorporate the regular expression function that checks the date's validity:

      if($date =~ /((^0?[1-9]|^1[0-2])\/(0?[1-9]|[1-2][0-9]|3[0-1])\/(19|20)?

      [0-9][0-9]\z)|((January|February|March|April|May|June|July|August|September

      |October|November|December) ([1-2][0-9]|3[0-1]|0?[1-9]), (19|20)?[0-9][0-9]\z)/)

      This code checks for invalid numerical representations of dates and any incorrectly spelled months in date input that is spelled out.

    • 4

      Print the error message if the date is invalid. Append the following code directly after the code you have already added:

      {

      print "You created a valid date";

      }

      else {

      print "The date you entered is invalid. Please re-enter your date value";

      }

Related Searches:

References

Comments

Related Ads

Featured