Tester:
package {
import flash.display.Sprite;
import org.actionscript.validations.*;
public class Form_Validation extends Sprite
{
protected var _lengthVal:IValidator = new StringLengthValidator(2, 4);
protected var _sameString:IValidator = new SameStringValidator(true);
protected var _emailVal:IValidator = new EmailValidator();
public function Form_Validation()
{
trace( "StringLengthValidator", _lengthVal.validate("heo") );
var animals:Array = ["dog", "Dog", "dog"];
trace( "SameStringValidator",_sameString.validate(animals) );
trace( "EmailValidator", _emailVal.validate("
[email protected]") );
}
}
}
package org.actionscript.validations
{
public interface IValidator
{
/**
* @return True if the validation passes
*/
function validate(input:*):Boolean;
}
}
package org.actionscript.validations
{
public class StringLengthValidator implements IValidator
{
protected var _min:int;
protected var _max:int;
public function StringLengthValidator(minLength:int=0, maxLength:int=int.MAX_VALUE)
{
_min = minLength;
_max = maxLength;
}
public function validate(input:*):Boolean
{
var min:Boolean = String(input).length >= _min;
var max:Boolean = String(input).length <= _max;
return min && max;
}
}
}
package org.actionscript.validations
{
import flash.errors.IllegalOperationError;
public class SameStringValidator implements IValidator
{
protected var _caseSensitive:Boolean;
protected var _inArr:Array
public function SameStringValidator(caseSensitive:Boolean=true)
{
_caseSensitive = caseSensitive;
}
public function validate(input:*):Boolean
{
var results:Boolean = true;
_inArr = input as Array;
if (!_inArr) throw new IllegalOperationError("SameStringValidator/validate @param input must be of type Array");
for (var i:int; i<_inArr.length; i++)
{
results = results && compare(i);
}
return results;
}
protected function compare(index:int):Boolean
{
var needle:String = _inArr[index] as String;
if (!needle) throw new IllegalOperationError("SameStringValidator/validate @param input must be of type Array/Vector.<String>");
var j:int=0;
if (!_caseSensitive)
{
needle = needle.toLowerCase();
for (j = index+1; j<_inArr.length; j++)
{
if (needle !== String(_inArr[j]).toLowerCase()) return false;
}
}
else
{
for (j = index+1; j<_inArr.length; j++)
{
if (needle !== String(_inArr[j])) return false;
}
}
return true;
}
}
}
package org.actionscript.validations
{
import org.actionscript.validations.ValidationExps;
public class EmailValidator implements IValidator
{
public function EmailValidator()
{
}
public function validate(input:*):Boolean
{
return String(input).search(ValidationExps.EMAIL) > -1;
}
}
}
package org.actionscript.validations
{
public class ValidationExps
{
/**
* Match a U.S. telephone number in the format (###) ###-####, where the area code is optional,
* the parentheses around the area code are optional and could be replaced with a dash, and
* there is optional spacing between the number groups.
*/
public static const PHONE:RegExp = /^(\(\s*\d{3}\s*\)|(\d{3}\s*-?))?\s*\d{3}\s*-?\s*\d{4}$/;
/**
* Match a U.S. telephone number, like the previous expression, except allow for an optional
* one- to five-digit extension specified with an "x", "ext", or "ext." and optional spacing.
*/
public static const PHONE_EXT:RegExp = /^(\(\s*\d{3}\s*\)|(\d{3}\s*-?))?\s*\d{3}\s*-?\s*\d{4}\s*((x|ext|ext\.)\s*\d{1,5})?$/;
/**
* Match a credit card number with four group of four digits separated by optional dashes
* and optional spacing between the groups.
*/
public static const CREDIT_CARD:RegExp = /^(\d{4}\s*\-?\s*){3}\d{4}$/;
/**
* Match U.S. currency starting with a $ and followed by any number with at most two
* optional decimal digits.
*/
public static const US_CURRENCY:RegExp = /^\$\d(\.\d{1,2})?$/;
/**
* Match an email address where the domain is not an IP address and may contain any number
* of optional subdomains. When creating this regex, it's a good idea to set the
* ignoreCase flag to true:
*/
public static const EMAIL_NO_IP:RegExp = /^[a-z0-9][-._a-z0-9]*@([a-z0-9][-_a-z0-9]*\.)+[a-z]{2,6}$/i;
/**
* Match an email address when the domain is either a domain name consisting of any number
* of optional subdomains or an IP address.
*/
public static const EMAIL:RegExp = /^[a-z0-9][-._a-z0-9]*@(([a-z0-9][-_a-z0-9]*\.)+[a-z]{2,6}|((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?))$/i;
/**
* Match a date in the format ##/##/####, where both the day and month value can be 1 or 2 digits,
* and the year can be either 2 digits or 4 digits when starting with 19 or 20.
*/
public static const SLASH_DATE:RegExp = /^\d{1,2}\/\d{1,2}\/(\d{2}|(19|20)\d{2})$/;
/**
* Match a five-digit U.S. Zip Code with an optional dash and four-digit extension.
*/
public static const US_ZIPCODE:RegExp = /^\d{5}(-\d{4})?$/;
/**
* Match a Canadian Postal Code in the format L#L #L# (where L is a letter). There is a
* restriction placed on the first letter in the Postal Code to ensure that a valid province,
* territory, or region is specified.
*/
public static const CA_POSTAL_CODE:RegExp = /^[ABCEGHJKLMNPRSTVXY]\d[A-Z] \d[A-Z]\d$/;
/**
* Match a social security number in the format ###-##-####, where the dashes are optional
* and the three groups can have optional spacing between them:
*/
public static const SSN:RegExp = /^\d{3}\s*-?\s*\d{2}\s*-?\s*\d{4}$/;
/**
* Match an IP address when you are only concerned that the address is formatted
* correctly with four groups of one to three digits separated by periods.
*/
public static const IP_ADDRESS:RegExp = /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/;
}
}