Minimum Age Validation Component

12 11 2009

The other day I took my first foray into the world of Custom Validators. I have to admit, I was intimidated at first. However, I needed to provide some validation to a form that I was writing. Rather than writing a sub-routine that was only usable once, I decided that I should make something a little more re-usable. Thus my MinimumAgeValidator was born. It can be used via straight AS, or in MXML. Code after the jump.

When using the validator, there are two options that you need to consider. First, the required minimum age and secondly if that age should be computed from a specific date (say by the end of a competition or sweepstakes) or from the current date. If you do not provide a date, the current date will be used to provide verification. The date, if provided, should be in the format MM/DD/YYYY. This format requirement also applies to the date being validated. The code formatting below sucks, so download the actual file.

/**
*
* This code is provided AS-IS with no warranty of any kind, it was originally written by the
* U.S. Institute of languages for use within Visual Link Languages. They have kindly allowed
* me to post this code here. You may do whatever you want with this code, including use it in
* commercial applications, or change it provided this notice remains in the source code.
* Retrived from captaincode.wordpress.com
**/
package com.captaincode.utils
{
import mx.validators.Validator;
import mx.validators.ValidationResult;

/**
* Provide basic validation to ensure that a date given (specifically a birthdate generally) meets
* the minumum age requirements.
*
**/
public class MinimumAgeValidator extends Validator
{
/**
* Hold the results of the validation attempts.
* @var Array results
* @private
*/
private var results:Array;

/**
* Calculate the age from a specified date. If ommited, the current date is used.
* It must be given in the format MM/DD/YYYY
* @var String fromDate;
*/
public var fromDate:String = "";

/**
* Set the minimum age required by the validator
* @var int minAge
*/
public var minAge:int = 0;

/**
* Set the error message to be displayed if the minimum age is not met.
* @var int minAge
*/
public var tooYoungError:String = "You are not old enough.";

/**
* Constructor. It only needs to call super(), as this validator is pretty basic.
*/
public function MinimumAgeValidator()
{
super();
}

/**
* This override function is where the guts of the validation take place.
* It is automatically envoked by the Validation framework to perfome the validation.
* @param Object value. Value contains the "data" from whatever property you have set the validator to function off of. i.e. .text on a TextInput component.
* @return Array An array of ValidationResult objects that contain the results of the validation attempt.
*/
override protected function doValidation(value:Object):Array {
var parts:Array = String(value).split("/");
var year:int = int(parts[2]);
var month:int = int(parts[0]);
var days:int = int(parts[1]);
var fYear:int;
var fMonth:int;
var fDay:int;

//Are we calculating on the from date?
if (fromDate == "")
{
var curDate:Date = new Date();
fYear = curDate.getFullYear();
fMonth = curDate.getMonth();
fDay = curDate.getDay();
}
else
{
var fParts:Array = fromDate.split("/");
if (fParts.length == 0)
{
results.push(new ValidationResult(true, null, "invalidDate", "The supplied fromDate is invalid."));
return results;
}
fYear= int(fParts[2]);
fMonth= int(fParts[0]);
fDay= int(fParts[1]);

if (isNaN(fYear))
{
results.push(new ValidationResult(true, null, "invalidDate", "The supplied fromDate year is invalid."));
return results;
}
if (isNaN(fMonth))
{
results.push(new ValidationResult(true, null, "invalidDate", "The supplied fromDate month is invalid."));
return results;
}
if (isNaN(fDay))
{
results.push(new ValidationResult(true, null, "invalidDate", "The supplied fromDate day is invalid."));
return results;
}
}

// Clear results Array.
results = [];

// Call base class doValidation().
results = super.doValidation(value);

// Return if there are errors.
if (results.length > 0)
return results;

//Do they meet the minimum age based soley on the year of their birth?
//Because, we only need to check the month and day if they were born on
//the minimum year to be accepted.
if (year == (fYear-minAge))
{
//Check months / days.
if (month < fMonth)
{
results.push(new ValidationResult(true, null, "tooYoung", tooYoungError));
return results;
}
if (month == fMonth && days < fDay)
{
results.push(new ValidationResult(true, null, "tooYoung", tooYoungError));
return results;
}
}
else if (year > (fYear-minAge))
{
//Not old enough.
results.push(new ValidationResult(true, null, "tooYoung", tooYoungError));
return results;
}

return results;
}
}
}

Advertisements

Actions

Information

One response

16 11 2009
Joe Billman

This is badness!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s




%d bloggers like this: