AS2: AgeChecker

View Documentation
Download Class

I had a need to do some age checking on the project that I'm currently working on. I figured there had to be something out there for this already and I googled away for an age checker in Flash. I found a very handy little function written by Calvin Ly and with his permission I simply wrapped this into a class and am posting it here for anyone who may need it in the future. Thanks Calvin!

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

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

2 Comments

You should add in some type of validation if nothing is entered. Currently you setup will return "true" if all or some of the parameters are missing.

Actionscript:
  1. if( $ageToCheck || $month || $day || $year == null )
  2.     {
  3.         return false;
  4.     }

no, the class is just meant to check for a proper age, it's up to you and your application to do the extra checking that goes into verifying if a user has entered a proper date or anything at all.

Leave a comment

(required)

(required)