AS3: AgeChecker

View Documentation
Download Class

This is a straight port of my AS2 AgeChecker (actually Calvin Ly's code, my class) for anyone interested.

Actionscript:
  1. package com.reintroducing.utils
  2. {
  3.     import flash.utils.getQualifiedClassName;
  4.    
  5.     /**
  6.      * A utility class that uses a static method to check whether a user's age is older than the age to check against.
  7.      *
  8.      * @author Calvin Ly [http://www.calvinly.com]
  9.      * @author Conversion to class by Matt Przybylski [http://www.reintroducing.com]
  10.      * @version 1.0
  11.      */
  12.     public class AgeChecker
  13.     {
  14. //- PRIVATE & PROTECTED VARIABLES -------------------------------------------------------------------------
  15.  
  16.        
  17.        
  18. //- PUBLIC & INTERNAL VARIABLES ---------------------------------------------------------------------------
  19.        
  20.        
  21.        
  22. //- CONSTRUCTOR -------------------------------------------------------------------------------------------
  23.    
  24.         public function AgeChecker():void
  25.         {
  26.            
  27.         }
  28.        
  29. //- PRIVATE & PROTECTED METHODS ---------------------------------------------------------------------------
  30.        
  31.        
  32.        
  33. //- PUBLIC & INTERNAL METHODS -----------------------------------------------------------------------------
  34.    
  35.         /**
  36.          * Checks whether the user's age is permissible or not.  The month and day can be passed in with leading zeros or not.
  37.          * The year should be passed in as the full year (ex: 1983).
  38.          *
  39.          * @param $ageToCheck The age you want to test against
  40.          * @param $month The user's input month
  41.          * @param $day The user's input day
  42.          * @param $year The user's input year
  43.          *
  44.          * @return A boolean value that states whether the age check has passed or not.
  45.          */
  46.         public static function checkAge($ageToCheck:int, $month:int, $day:int, $year:int):Boolean
  47.         {
  48.             var todayDate:Date = new Date();
  49.             var currentMonth:int = (todayDate.getMonth() + 1);
  50.             var currentDay:int = todayDate.getDate();
  51.             var currentYear:int = todayDate.getFullYear();
  52.            
  53.             var userMonth:int = $month;
  54.             var userDay:int = $day;
  55.             var userYear:int = $year;
  56.            
  57.             var yearDiff:int = (currentYear - userYear);
  58.            
  59.             if (yearDiff == $ageToCheck)
  60.             {
  61.                 // AGE IS EQUAL to VALID AGE ... need to check month and day
  62.                 var monthDiff:int = (currentMonth - userMonth);
  63.                
  64.                 if (monthDiff == 0)
  65.                 {
  66.                     // MONTH IS EQUAL ... need to check day
  67.                     var dayDiff:int = currentDay - userDay;
  68.                    
  69.                     if (dayDiff>= 0)
  70.                     {
  71.                         // DAY IS EQUAL OR GREATER .. PASS
  72.                         return true;
  73.                     }
  74.                     else
  75.                     {
  76.                         // DAY INVALID ... too young
  77.                         return false;
  78.                     }
  79.                        
  80.                 }
  81.                 else if (monthDiff <0)
  82.                 {
  83.                     // MONTH INVALID ... too young
  84.                     return false;
  85.                 }
  86.                 else
  87.                 {
  88.                     // AGE PASS
  89.                     return true;
  90.                 }
  91.             }
  92.             else if (yearDiff <$ageToCheck)
  93.             {
  94.                 // YEAR INVALID ... too young
  95.                 return false;
  96.             }
  97.             else
  98.             {
  99.                 // OVER AGE in YEARS
  100.                 return true;
  101.             }
  102.         }
  103.    
  104. //- EVENT HANDLERS ----------------------------------------------------------------------------------------
  105.    
  106.        
  107.    
  108. //- GETTERS & SETTERS -------------------------------------------------------------------------------------
  109.    
  110.        
  111.    
  112. //- HELPERS -----------------------------------------------------------------------------------------------
  113.    
  114.         public function toString():String
  115.         {
  116.             return getQualifiedClassName(this);
  117.         }
  118.    
  119. //- END CLASS ---------------------------------------------------------------------------------------------
  120.     }
  121. }

If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.

1 Comment

great function, thanks a lot

Leave a comment

(required)

(required)