- package oreilly.cookbook {
- import mx.validators.ValidationResult;
- import mx.validators.Validator;
- public class SouthAmericanValidator extends Validator {
- // store all of our countries and their postal codes in a hash table
- private var countryHash:Object = {"Argentina":/[a-zA-Z]\\d{4}[a-zA-Z]{3}/,
- "Brazil":/\\d{5}-\\d{3}/, "Mexico":/\\d{5}/, "Bolivia":/\\d{4}/,
- "Chile":/\\d{7}/, "Paraguay":/\\d{4}/, "Uruguay":/\\d{5}/};
- private var results:Array;
- private var _country:String;
- public function SouthAmericanValidator() {
- super();
- }
- public function set country(str:String):void {
- _country = str;
- trace(_country);
- }
- // define the doValidation() method
- override protected function doValidation(value:Object):Array {
- // clear results Array
- results = [];
- // if we don't have a country set, we return an error
- if(_country == "") {
- var err:ValidationResult = new ValidationResult(true, "", "",
- "Please Select a Country");
- results.push(err);
- return results;
- } else {
- // if it's a country that doesn't have a zip code, we return
- // w/o an error
- if(countryHash[_country] != null) {
- // read from our hash table and get the correct RegExp
- var regEx:RegExp = countryHash[_country];
- if(regEx.test(value as String)) {
- return results;
- } else {
- // if the postal code doesn't validate, return an error
- var err:ValidationResult = new ValidationResult(true, "",
- "", "Please Enter A Correct Postal Code");
- results.push(err);
- }
- } else {
- return results;
- }
- }
- return results;
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/15112012371.html
来源: http://www.codesnippet.cn/detail/15112012371.html