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:
-
/**
-
* A utility class that uses a static method to check whether a user's age is older than the age to check against.
-
*
-
* @usage
-
* <code>
-
* <pre>
-
import com.reintroducing.utils.AgeChecker;
-
var isOldEnough:Boolean = AgeChecker.checkAge(21, 01, 19, 1983);
-
* </pre>
-
* </code>
-
*
-
* @author Calvin Ly [http://www.calvinly.com]
-
* @author Conversion to class by Matt Przybylski [http://www.reintroducing.com]
-
* @version 1.0
-
*/
-
-
class com.reintroducing.utils.AgeChecker
-
{
-
//- PRIVATE VARIABLES -------------------------------------------------------------------------------------
-
-
-
-
//- PUBLIC VARIABLES --------------------------------------------------------------------------------------
-
-
-
-
//- CONSTRUCTOR -------------------------------------------------------------------------------------------
-
-
public function AgeChecker()
-
{
-
-
}
-
-
//- PRIVATE METHODS ---------------------------------------------------------------------------------------
-
-
-
-
//- PUBLIC METHODS ----------------------------------------------------------------------------------------
-
-
/**
-
* Checks whether the user's age is permissible or not. The month and day can be passed in with leading zeros or not.
-
* The year should be passed in as the full year (ex: 1983).
-
*
-
* @usage <pre><code>AgeChecker.checkAge($ageToCheck, $month, $day, $year)</code></pre>
-
*
-
* @param $ageToCheck The age you want to test against
-
* @param $month The user's input month
-
* @param $day The user's input day
-
* @param $year The user's input year
-
*
-
* @return A boolean value that states whether the age check has passed or not.
-
*/
-
-
public static function checkAge($ageToCheck:Number, $month:Number, $day:Number, $year:Number):Boolean
-
{
-
var todayDate:Date = new Date();
-
var currentMonth:Number = (todayDate.getMonth() + 1);
-
var currentDay:Number = todayDate.getDate();
-
var currentYear:Number = todayDate.getFullYear();
-
-
var userMonth:Number = $month;
-
var userDay:Number = $day;
-
var userYear:Number = $year;
-
-
var yearDiff:Number = (currentYear - userYear);
-
-
if (yearDiff == $ageToCheck)
-
{
-
// AGE IS EQUAL to VALID AGE ... need to check month and day
-
var monthDiff:Number = (currentMonth - userMonth);
-
-
if (monthDiff == 0)
-
{
-
// MONTH IS EQUAL ... need to check day
-
var dayDiff:Number = currentDay - userDay;
-
-
if (dayDiff>= 0)
-
{
-
// DAY IS EQUAL OR GREATER .. PASS
-
return true;
-
}
-
else
-
{
-
// DAY INVALID ... too young
-
return false;
-
}
-
-
}
-
else if (monthDiff <0)
-
{
-
// MONTH INVALID ... too young
-
return false;
-
}
-
else
-
{
-
// AGE PASS
-
return true;
-
}
-
}
-
else if (yearDiff <$ageToCheck)
-
{
-
// YEAR INVALID ... too young
-
return false;
-
}
-
else
-
{
-
// OVER AGE in YEARS
-
return true;
-
}
-
}
-
-
//- EVENT HANDLERS ----------------------------------------------------------------------------------------
-
-
-
-
//- GETTERS & SETTERS -------------------------------------------------------------------------------------
-
-
-
-
//- HELPERS -----------------------------------------------------------------------------------------------
-
-
public function toString():String
-
{
-
return "com.reintroducing.utils.AgeChecker";
-
}
-
-
//- END CLASS ---------------------------------------------------------------------------------------------
-
}
If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.













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.