Return-Path:
Postcode formats consists of the letters C, N, A and spaces or hyphens
* formatString
or
* formats
property.
- *
+ *
*
*
Country codes one be one or two digits.
- * + * *For example "NNNN" is a four digit numeric postcode, "CCNNNN" is country code * followed by four digits and "AA NNNN" is two letters, followed by a space then * followed by four digits.
- * + * *More than one format can be specified by setting the formats
* property to an array of format strings.
Spaces and hypens will be added if missing to format the postcode correctly.
* - *If an error occurs, an empty String is returned and a String that
- * describes the error is saved to the error
property.
+ *
If an error occurs, an empty String is returned and a String that
+ * describes the error is saved to the error
property.
* The error
property can have one of the following values:
*
"invalidFormat"
means the format constants an invalid
@@ -62,25 +62,25 @@ import mx.validators.ValidationResult;
* "invalidChar"
means the postcode contains an invalid
* character.Fullwidth numbers and letters are supported in postcodes by ignoring character
* width via the flash.globalization.Collator
equals
method.
The <mx:PostCodeFormatter>
tag
* inherits all of the tag attributes of its superclass,
* and adds the following tag attributes:
* <mx:PostCodeFormatter * formatString="NNNNN" * formats="['NNNNN', 'NNNNN-NNNN']" * /> *- * + * * @see mx.validators.PostCodeValidator - * + * * @langversion 3.0 * @playerversion Flash 10.2 * @productversion ApacheFlex 4.8 @@ -88,224 +88,209 @@ import mx.validators.ValidationResult; public class PostCodeFormatter extends Formatter { include "../core/Version.as"; - - //-------------------------------------------------------------------------- - // - // Constructor - // - //-------------------------------------------------------------------------- - - /** - * Constructor. - * - * @langversion 3.0 - * @playerversion Flash 10.2 - * @productversion ApacheFlex 4.8 - */ - public function PostCodeFormatter() - { - super(); - } - - //-------------------------------------------------------------------------- - // - // Properties - // - //-------------------------------------------------------------------------- - - //---------------------------------- - // formats - //---------------------------------- - - /** - * @private - * An array of the postcode formats to check against. - */ - private var _formats:Array = []; - - - [Inspectable(category="General", defaultValue="null")] - - /** - * Format string to format the postcode in. - * - *
The format string consists of the letters C, N, A and spaces - * or hyphens: - *
Use for locales where more than one format is required. - * eg en_UK
- * - *See formatString
for format of the format
- * strings.
formatString
- * or formats
.
- *
- * If the value cannot be formatted this method returns an empty String
- * and write a description of the error to the error
property.
error
property.
- *
- * @langversion 3.0
- * @playerversion Flash 10.2
- * @productversion ApacheFlex 4.8
- */
- override public function format(value:Object):String
- {
- var postCode:String = value as String;
- var formatted:String = "";
- var validator:PostCodeValidator = new PostCodeValidator();
- var errors:Array;
-
- error = "";
-
- validator.formats = formats;
- errors = PostCodeValidator.validatePostCode(validator, postCode, null);
-
- // Valid postcode no need for formatting
- if (errors.length == 0)
- {
- return postCode?postCode:"";
- }
-
- // Check and add missing (or convert) padding characters
- for each (var format:String in formats)
- {
- var condensedPostcode:String = condensedFormat(postCode);
- var condensedFormat:String = condensedFormat(format);
- var char:String;
- var length:int = format.length;
- var condensedErrors:Array;
-
- validator.format = condensedFormat;
-
- condensedErrors = PostCodeValidator.validatePostCode(validator, condensedPostcode, null);
-
- if (condensedErrors.length == 0)
- {
- var pos:int = 0;
-
- for (var i:int = 0; i < length; i++)
- {
- char = format.charAt(i);
-
- if (PostCodeValidator.FORMAT_SPACERS.indexOf(char) >= 0)
- {
- formatted += char;
- }
- else
- {
- formatted += condensedPostcode.charAt(pos++);
- }
- }
- //TODO may want to return the longest match?
- errors = [];
- break;
- }
- }
-
- if (errors.length > 0)
- {
- error = (errors[0] as ValidationResult).errorCode;
- }
-
- return formatted;
- }
-
- /**
- * @private
- *
- * Take a format or paostCode and strip all spacing characters
- * out of it.
- *
- */
- protected function condensedFormat(postCode:String):String
- {
- var condensed:String = postCode;
- var length:int;
-
- if (postCode)
- {
- length = postCode.length;
- }
-
- for (var i:int = 0; i < length; i++)
- {
- var char:String = PostCodeValidator.FORMAT_SPACERS.charAt(i);
-
- condensed = condensed.split(char).join("");
- }
-
- return condensed;
- }
-
+
+ //--------------------------------------------------------------------------
+ //
+ // Constructor
+ //
+ //--------------------------------------------------------------------------
+
+ /**
+ * Constructor.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @productversion ApacheFlex 4.8
+ */
+ public function PostCodeFormatter()
+ {
+ super();
+ }
+
+ //--------------------------------------------------------------------------
+ //
+ // Properties
+ //
+ //--------------------------------------------------------------------------
+
+ //----------------------------------
+ // formats
+ //----------------------------------
+
+ /**
+ * @private
+ * An array of the postcode formats to check against.
+ */
+ private var _formats:Array = [];
+
+
+ [Inspectable(category = "General", defaultValue = "null")]
+
+ /**
+ * Format string to format the postcode in.
+ *
+ * The format string consists of the letters C, N, A and spaces + * or hyphens: + *
Use for locales where more than one format is required. + * eg en_UK
+ * + *See formatString
for format of the format
+ * strings.
formatString
+ * or formats
.
+ *
+ * If the value cannot be formatted this method returns an empty String
+ * and write a description of the error to the error
property.
error
property.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @productversion ApacheFlex 4.8
+ */
+ override public function format(value:Object):String
+ {
+ var postCode:String = value as String;
+ var formatted:String = "";
+ var validator:PostCodeValidator = new PostCodeValidator();
+ var errors:Array;
+
+ error = "";
+
+ validator.formats = formats;
+ errors = PostCodeValidator.validatePostCode(validator, postCode, null);
+
+ // Valid postcode no need for formatting
+ if (errors.length == 0)
+ return postCode ? postCode : "";
+
+ // Check and add missing (or convert) padding characters
+ for each (var format:String in formats)
+ {
+ var condensedPostcode:String = condensedFormat(postCode);
+ var condensedFormat:String = condensedFormat(format);
+ var char:String;
+ var length:int = format.length;
+ var condensedErrors:Array;
+
+ validator.format = condensedFormat;
+
+ condensedErrors = PostCodeValidator.validatePostCode(validator, condensedPostcode, null);
+
+ if (condensedErrors.length == 0)
+ {
+ var pos:int = 0;
+
+ for (var i:int = 0; i < length; i++)
+ {
+ char = format.charAt(i);
+
+ if (PostCodeValidator.FORMAT_SPACERS.indexOf(char) >= 0)
+ formatted += char;
+ else
+ formatted += condensedPostcode.charAt(pos++);
+ }
+
+ //TODO may want to return the longest match?
+ errors = [];
+ break;
+ }
+ }
+
+ if (errors.length > 0)
+ error = (errors[0] as ValidationResult).errorCode;
+
+ return formatted;
+ }
+
+ /**
+ * @private
+ *
+ * Take a format or paostCode and strip all spacing characters
+ * out of it.
+ *
+ */
+ protected function condensedFormat(postCode:String):String
+ {
+ var condensed:String = postCode;
+ var length:int;
+
+ if (postCode)
+ length = postCode.length;
+
+ for (var i:int = 0; i < length; i++)
+ {
+ var char:String = PostCodeValidator.FORMAT_SPACERS.charAt(i);
+
+ condensed = condensed.split(char).join("");
+ }
+
+ return condensed;
+ }
+
}
}
Modified: incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as
URL: http://svn.apache.org/viewvc/incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as?rev=1299646&r1=1299645&r2=1299646&view=diff
==============================================================================
--- incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as (original)
+++ incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as Mon Mar 12 12:21:16 2012
@@ -25,46 +25,46 @@ import flash.globalization.LocaleID;
import flash.globalization.StringTools;
import mx.resources.IResourceManager;
-import mx.resources.ResourceManager;
+import mx.resources.ResourceManager;
[ResourceBundle("validators")]
/**
* The PostCodeValidator class validates that a String
* has the correct length and format for a post code.
- *
+ *
* Postcode formats consists of the letters C, N, A and spaces or hyphens *
Country codes one be one or two digits.
- * + * *For example "NNNN" is a four digit numeric postcode, "CCNNNN" is country code * followed by four digits and "AA NNNN" is two letters, followed by a space then * followed by four digits.
- * + * *More than one format can be specified by setting the formats
* property to an array of format Strings.
The validator can suggest postcode formats for small set of know locales by calling the
* suggestFormat
method.
Postcodes can be further validated by setting the extraValidation
* property to a user defined method that performs further checking on the postcode
* digits.
Fullwidth numbers and letters are supported in postcodes by ignoring character
* width via the flash.globalization.Collator
equals
method.
The <mx:PostCodeValidator>
tag
* inherits all of the tag attributes of its superclass,
* and adds the following tag attributes:
* <mx:PostCodeValidator * countryCode="CC" @@ -72,13 +72,13 @@ import mx.resources.ResourceManager; * formats="['NNNNN', 'NNNNN-NNNN']" * wrongFromatError="The postcode code must be correctly formatted." * invalidFormatError="The postcode format string is incorrect." - * invalidCharError="The postcode contains invalid characters." - * wrongLengthError="The postcode is the wrong length." + * invalidCharError="The postcode contains invalid characters." + * wrongLengthError="The postcode is the wrong length." * /> *- * + * * @see mx.formatters.PostCodeFormatter - * + * * @langversion 3.0 * @playerversion Flash 10.2 * @productversion ApacheFlex 4.8 @@ -86,47 +86,51 @@ import mx.resources.ResourceManager; public class PostCodeValidator extends Validator { include "../core/Version.as"; - - /** - * Value
errorCode
of a ValidationResult is set to when
- * the postcode contains an invalid charater.
- */
- public static const ERROR_INVALID_CHAR:String = "invalidChar";
-
- /**
- * Value errorCode
of a ValidationResult is set to when
- * the postcode is of the wrong length.
- */
- public static const ERROR_WRONG_LENGTH:String = "wrongLength";
- /**
- * Value errorCode
of a ValidationResult is set to when
- * the postcode is of the wrong format.
- */
- public static const ERROR_WRONG_FORMAT:String = "wrongFormat";
-
- /**
- * Value errorCode
of a ValidationResult is set to when
- * the format contains unknown format characters.
- */
- public static const ERROR_INCORRECT_FORMAT:String = "incorrectFormat";
-
- /**
- * Symbol used in postcode formats representing a single digit.
- */
- public static const FORMAT_NUMBER:String = "N";
- /**
- * Symbol used in postcode formats representing a single character.
- */
- public static const FORMAT_LETTER:String = "A";
- /**
- * Symbol used in postcode formats representing a letter of a country
- * code.
- */
- public static const FORMAT_COUNTRY_CODE:String = "C";
- /**
- * Valid spacer character in postcode formats.
- */
- public static const FORMAT_SPACERS:String = " -";
+
+ /**
+ * Value errorCode
of a ValidationResult is set to when
+ * the postcode contains an invalid charater.
+ */
+ public static const ERROR_INVALID_CHAR:String = "invalidChar";
+
+ /**
+ * Value errorCode
of a ValidationResult is set to when
+ * the postcode is of the wrong length.
+ */
+ public static const ERROR_WRONG_LENGTH:String = "wrongLength";
+
+ /**
+ * Value errorCode
of a ValidationResult is set to when
+ * the postcode is of the wrong format.
+ */
+ public static const ERROR_WRONG_FORMAT:String = "wrongFormat";
+
+ /**
+ * Value errorCode
of a ValidationResult is set to when
+ * the format contains unknown format characters.
+ */
+ public static const ERROR_INCORRECT_FORMAT:String = "incorrectFormat";
+
+ /**
+ * Symbol used in postcode formats representing a single digit.
+ */
+ public static const FORMAT_NUMBER:String = "N";
+
+ /**
+ * Symbol used in postcode formats representing a single character.
+ */
+ public static const FORMAT_LETTER:String = "A";
+
+ /**
+ * Symbol used in postcode formats representing a letter of a country
+ * code.
+ */
+ public static const FORMAT_COUNTRY_CODE:String = "C";
+
+ /**
+ * Valid spacer character in postcode formats.
+ */
+ public static const FORMAT_SPACERS:String = " -";
//--------------------------------------------------------------------------
//
@@ -134,270 +138,229 @@ public class PostCodeValidator extends V
//
//--------------------------------------------------------------------------
- /**
- * @private
- * Simulate String.indexOf but ignore wide characters.
- * TODO move to StringValidator or Collator?
- *
- * @return Index of char in string or -1 if char not in string.
- *
- */
- protected function indexOf(string:String, char:String):int {
- var length:int = string.length;
- var collate:Collator = new Collator(LocaleID.DEFAULT);
-
- collate.ignoreCharacterWidth = true;
-
- for (var i:int =0 ; i < length; i++)
- {
- if (collate.equals(string.charAt(i), char))
- {
- return i;
- }
- }
-
- return -1;
- }
-
- /**
- * @private
- * Compares if two characters are equal ignoring wide characters.
- * TODO move to StringValidator or Collator?
- *
- * @return True is charA is the same as charB, false if they are not.
- *
- */
- protected function equals(charA:String, charB:String):Boolean {
- var collate:Collator = new Collator(LocaleID.DEFAULT);
-
- collate.ignoreCharacterWidth = true;
-
- return collate.equals(charA, charB);
- }
-
- /**
- * @private
- *
- * @param char to check
- * @return True if the char is not a valid format character.
- *
- */
- protected function notFormatChar(char:String):Boolean
- {
- return indexOf(FORMAT_SPACERS, char) == -1
- && char != FORMAT_NUMBER
- && char != FORMAT_LETTER
- && char != FORMAT_COUNTRY_CODE;
- }
-
- /**
- * @private
- *
- * @param char to check
- * @return True if the char is not a valid digit.
- *
- */
- protected function noDecimalDigits(char:String):Boolean
- {
- return indexOf(DECIMAL_DIGITS, char) == -1;
- }
-
- /**
- * @private
- *
- * @param char to check
- * @return True if the char is not a valid letter.
- *
- */
- protected function noRomanLetters(char:String):Boolean
- {
- return indexOf(ROMAN_LETTERS, char) == -1;
- }
-
- /**
- * @private
- *
- * @param char to check
- * @return True if the char is not a valid spacer.
- *
- */
- protected function noSpacers(char:String):Boolean
- {
- return indexOf(FORMAT_SPACERS, char) == -1;
- }
-
- /**
- * @private
- *
- * A wrong format ValidationResult is added to the results array
- * if the extraValidation user supplied function returns an error.
- * An error is added when there is a user defined issue with the
- * supplied postCode.
- *
- */
- protected function userValidationResults(validator:PostCodeValidator,
- baseField:String,
- postCode:String,
- results:Array):void
- {
- if (validator && validator.extraValidation != null)
- {
- var extraError:String = validator.extraValidation(postCode);
-
- if (extraError)
- {
- results.push(new ValidationResult(
- true, baseField, ERROR_WRONG_FORMAT, extraError));
- }
- }
- }
-
- /**
- * @private
- *
- * Based on flags in the error object new ValidationResults are
- * added the the results array.
- *
- * Note that the only first ValidationResult is typically shown
- * to the user in validation errors.
- *
- */
- protected function errorValidationResults(validator:PostCodeValidator,
- baseField:String,
- error:Object,
- results:Array):void
- {
- if (error) {
- if (error.incorrectFormat)
- {
- results.push(new ValidationResult(
- true, baseField, ERROR_INCORRECT_FORMAT,
- validator.incorrectFormatError));
- }
- if (error.invalidChar)
- {
- results.push(new ValidationResult(
- true, baseField, ERROR_INVALID_CHAR,
- validator.invalidCharError));
- }
-
- if (error.wrongLength)
- {
- results.push(new ValidationResult(
- true, baseField, ERROR_WRONG_LENGTH,
- validator.wrongLengthError));
- }
-
- if (error.invalidFormat)
- {
- results.push(new ValidationResult(
- true, baseField, ERROR_WRONG_FORMAT,
- validator.wrongFormatError));
- }
- }
- }
-
- /**
- * @private
- *
- * Check thats a postCode is valid and matches a certain format.
- *
- * @return An error object containing flags for each type of error
- * and an indication of invalidness (used later to sort errors).
- *
- */
- protected function checkPostCodeFormat(postCode:String,
- format:String,
- countryCode:String):Object
- {
- var invalidChar:Boolean;
- var invalidFormat:Boolean;
- var wrongLength:Boolean;
- var incorrectFormat:Boolean;
- var formatLength:int;
- var postCodeLength:int;
- var noChars:int;
- var countryIndex:int;
-
- if (format)
- {
- formatLength = format.length;
- }
-
- if (formatLength == 0)
- {
- incorrectFormat = true;
- }
-
- if (postCode)
- {
- postCodeLength = postCode.length;
- }
-
- noChars = Math.min(formatLength,postCodeLength);
-
- for (var postcodeIndex:int = 0; postcodeIndex < noChars; postcodeIndex++)
- {
- var char:String = postCode.charAt(postcodeIndex);
- var formatChar:String = format.charAt(postcodeIndex);
-
- if (postcodeIndex < postCodeLength)
- {
- char = postCode.charAt(postcodeIndex);
- }
-
- if (notFormatChar(formatChar))
- {
- incorrectFormat = true;
- }
-
- if (noDecimalDigits(char) && noRomanLetters(char) && noSpacers(char))
- {
- if (!countryCode || indexOf(countryCode,char) == -1)
- {
- invalidChar = true;
- }
- }
- else if (formatChar == FORMAT_NUMBER && noDecimalDigits(char))
- {
- invalidFormat = true;
- }
- else if (formatChar == FORMAT_LETTER && noRomanLetters(char))
- {
- invalidFormat = true;
- }
- else if (formatChar == FORMAT_COUNTRY_CODE)
- {
- if (countryIndex >= 2 || !countryCode || !equals(char, countryCode.charAt(countryIndex)))
- {
- invalidFormat = true;
- }
- countryIndex++;
- }
- else if (indexOf(FORMAT_SPACERS, formatChar) >= 0 && !equals(formatChar, char)) {
- invalidFormat = true;
- }
- }
-
- wrongLength = (postCodeLength != formatLength);
-
- // We want invalid char and invalid format errors to show in preference
- // so give wrong length errors a higher value
- if (incorrectFormat || invalidFormat || invalidChar || wrongLength)
- {
- return {invalidFormat:invalidFormat,
- incorrectFormat:incorrectFormat,
- invalidChar:invalidChar,
- wrongLength:wrongLength,
- invalidness:Number(invalidFormat) + Number(invalidChar)
- + Number(incorrectFormat) + Number(wrongLength) * 1.5};
- }
- else
- {
- return null;
- }
- }
-
+ /**
+ * @private
+ * Simulate String.indexOf but ignore wide characters.
+ * TODO move to StringValidator or Collator?
+ *
+ * @return Index of char in string or -1 if char not in string.
+ *
+ */
+ protected function indexOf(string:String, char:String):int
+ {
+ var length:int = string.length;
+ var collate:Collator = new Collator(LocaleID.DEFAULT);
+
+ collate.ignoreCharacterWidth = true;
+
+ for (var i:int = 0; i < length; i++)
+ if (collate.equals(string.charAt(i), char))
+ return i;
+
+ return -1;
+ }
+
+ /**
+ * @private
+ * Compares if two characters are equal ignoring wide characters.
+ * TODO move to StringValidator or Collator?
+ *
+ * @return True is charA is the same as charB, false if they are not.
+ *
+ */
+ protected function equals(charA:String, charB:String):Boolean
+ {
+ var collate:Collator = new Collator(LocaleID.DEFAULT);
+
+ collate.ignoreCharacterWidth = true;
+
+ return collate.equals(charA, charB);
+ }
+
+ /**
+ * @private
+ *
+ * @param char to check
+ * @return True if the char is not a valid format character.
+ *
+ */
+ protected function notFormatChar(char:String):Boolean
+ {
+ return indexOf(FORMAT_SPACERS, char) == -1 && char != FORMAT_NUMBER &&
+ char != FORMAT_LETTER && char != FORMAT_COUNTRY_CODE;
+ }
+
+ /**
+ * @private
+ *
+ * @param char to check
+ * @return True if the char is not a valid digit.
+ *
+ */
+ protected function noDecimalDigits(char:String):Boolean
+ {
+ return indexOf(DECIMAL_DIGITS, char) == -1;
+ }
+
+ /**
+ * @private
+ *
+ * @param char to check
+ * @return True if the char is not a valid letter.
+ *
+ */
+ protected function noRomanLetters(char:String):Boolean
+ {
+ return indexOf(ROMAN_LETTERS, char) == -1;
+ }
+
+ /**
+ * @private
+ *
+ * @param char to check
+ * @return True if the char is not a valid spacer.
+ *
+ */
+ protected function noSpacers(char:String):Boolean
+ {
+ return indexOf(FORMAT_SPACERS, char) == -1;
+ }
+
+ /**
+ * @private
+ *
+ * A wrong format ValidationResult is added to the results array
+ * if the extraValidation user supplied function returns an error.
+ * An error is added when there is a user defined issue with the
+ * supplied postCode.
+ *
+ */
+ protected function userValidationResults(validator:PostCodeValidator, baseField:String,
+ postCode:String, results:Array):void
+ {
+ if (validator && validator.extraValidation != null)
+ {
+ var extraError:String = validator.extraValidation(postCode);
+
+ if (extraError)
+ results.push(new ValidationResult(true, baseField, ERROR_WRONG_FORMAT, extraError));
+ }
+ }
+
+ /**
+ * @private
+ *
+ * Based on flags in the error object new ValidationResults are
+ * added the the results array.
+ *
+ * Note that the only first ValidationResult is typically shown
+ * to the user in validation errors.
+ *
+ */
+ protected function errorValidationResults(validator:PostCodeValidator, baseField:String,
+ error:Object, results:Array):void
+ {
+ if (error)
+ {
+ if (error.incorrectFormat)
+ results.push(new ValidationResult(true, baseField, ERROR_INCORRECT_FORMAT,
+ validator.incorrectFormatError));
+ if (error.invalidChar)
+ results.push(new ValidationResult(true, baseField, ERROR_INVALID_CHAR,
+ validator.invalidCharError));
+
+ if (error.wrongLength)
+ results.push(new ValidationResult(true, baseField, ERROR_WRONG_LENGTH,
+ validator.wrongLengthError));
+
+ if (error.invalidFormat)
+ results.push(new ValidationResult(true, baseField, ERROR_WRONG_FORMAT,
+ validator.wrongFormatError));
+ }
+ }
+
+ /**
+ * @private
+ *
+ * Check thats a postCode is valid and matches a certain format.
+ *
+ * @return An error object containing flags for each type of error
+ * and an indication of invalidness (used later to sort errors).
+ *
+ */
+ protected function checkPostCodeFormat(postCode:String, format:String,
+ countryCode:String):Object
+ {
+ var invalidChar:Boolean;
+ var invalidFormat:Boolean;
+ var wrongLength:Boolean;
+ var incorrectFormat:Boolean;
+ var formatLength:int;
+ var postCodeLength:int;
+ var noChars:int;
+ var countryIndex:int;
+
+ if (format)
+ formatLength = format.length;
+
+ if (formatLength == 0)
+ incorrectFormat = true;
+
+ if (postCode)
+ postCodeLength = postCode.length;
+
+ noChars = Math.min(formatLength, postCodeLength);
+
+ for (var postcodeIndex:int = 0; postcodeIndex < noChars; postcodeIndex++)
+ {
+ var char:String = postCode.charAt(postcodeIndex);
+ var formatChar:String = format.charAt(postcodeIndex);
+
+ if (postcodeIndex < postCodeLength)
+ char = postCode.charAt(postcodeIndex);
+
+ if (notFormatChar(formatChar))
+ incorrectFormat = true;
+
+ if (noDecimalDigits(char) && noRomanLetters(char) && noSpacers(char))
+ {
+ if (!countryCode || indexOf(countryCode, char) == -1)
+ invalidChar = true;
+ }
+ else if (formatChar == FORMAT_NUMBER && noDecimalDigits(char))
+ {
+ invalidFormat = true;
+ }
+ else if (formatChar == FORMAT_LETTER && noRomanLetters(char))
+ {
+ invalidFormat = true;
+ }
+ else if (formatChar == FORMAT_COUNTRY_CODE)
+ {
+ if (countryIndex >= 2 || !countryCode ||
+ !equals(char, countryCode.charAt(countryIndex)))
+ invalidFormat = true;
+ countryIndex++;
+ }
+ else if (indexOf(FORMAT_SPACERS, formatChar) >= 0 && !equals(formatChar, char))
+ {
+ invalidFormat = true;
+ }
+ }
+
+ wrongLength = (postCodeLength != formatLength);
+
+ // We want invalid char and invalid format errors to show in preference
+ // so give wrong length errors a higher value
+ if (incorrectFormat || invalidFormat || invalidChar || wrongLength)
+ return { invalidFormat: invalidFormat, incorrectFormat: incorrectFormat,
+ invalidChar: invalidChar, wrongLength: wrongLength,
+ invalidness: Number(invalidFormat) + Number(invalidChar) +
+ Number(incorrectFormat) + Number(wrongLength) * 1.5 };
+ else
+ return null;
+ }
+
/**
* Convenience method for calling a validator.
* Each of the standard Flex validators has a similar convenience method.
@@ -411,56 +374,55 @@ public class PostCodeValidator extends V
* For example, if the value
parameter specifies value.postCode,
* the baseField
value is "postCode"
.
*
- * @return An Array of ValidationResult objects, with one ValidationResult
- * object for each field examined by the validator.
+ * @return An Array of ValidationResult objects, with one ValidationResult
+ * object for each field examined by the validator.
*
* @see mx.validators.ValidationResult
*
- * @langversion 3.0
- * @playerversion Flash 10.2
- * @productversion ApacheFlex 4.8
- */
- public static function validatePostCode(validator:PostCodeValidator,
- postCode:String,
- baseField:String):Array
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @productversion ApacheFlex 4.8
+ */
+ public static function validatePostCode(validator:PostCodeValidator, postCode:String,
+ baseField:String):Array
{
- var numberFormats:int;
- var errors:Array = [];
+ var numberFormats:int;
+ var errors:Array = [];
var results:Array = [];
- if (!validator)
- {
- return [];
- }
-
- numberFormats = validator.formats.length;
- for (var formatIndex:int = 0; formatIndex < numberFormats; formatIndex++)
- {
- var error:Object = validator.checkPostCodeFormat(postCode,
- validator.formats[formatIndex], validator.countryCode);
-
- if (error)
- {
- errors.push(error);
- }
- else
- {
- errors = [];
- break;
- }
- }
-
- // return result with least number of errors
- errors.sortOn("invalidness", Array.NUMERIC);
-
- validator.userValidationResults(validator, baseField, postCode, results);
-
- // TODO return/remember closest format or place in results?
- validator.errorValidationResults(validator, baseField, errors[0], results);
-
+ if (!validator)
+ return [];
+
+ numberFormats = validator.formats.length;
+
+ for (var formatIndex:int = 0; formatIndex < numberFormats; formatIndex++)
+ {
+ var error:Object =
+ validator.checkPostCodeFormat(postCode, validator.formats[formatIndex],
+ validator.countryCode);
+
+ if (error)
+ {
+ errors.push(error);
+ }
+ else
+ {
+ errors = [];
+ break;
+ }
+ }
+
+ // return result with least number of errors
+ errors.sortOn("invalidness", Array.NUMERIC);
+
+ validator.userValidationResults(validator, baseField, postCode, results);
+
+ // TODO return/remember closest format or place in results?
+ validator.errorValidationResults(validator, baseField, errors[0], results);
+
return results;
}
-
+
//--------------------------------------------------------------------------
//
// Constructor
@@ -469,10 +431,10 @@ public class PostCodeValidator extends V
/**
* Constructor.
- *
- * @langversion 3.0
- * @playerversion Flash 10.2
- * @productversion ApacheFlex 4.8
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @productversion ApacheFlex 4.8
*/
public function PostCodeValidator()
{
@@ -484,137 +446,129 @@ public class PostCodeValidator extends V
// Properties
//
//--------------------------------------------------------------------------
-
- /**
- * @private
- * The two letter country code used in some postcode formats
- */
- private var _countryCode:String;
-
- /**
- * @private
- * An array of the postcode formats to check against.
- */
- private var _formats:Array = [];
-
- /**
- * Valid postcode format that postcodes will be compaired against.
- *
- * The format string consists of the letters C, N, A and spaces - * or hyphens: - *
Use for locales where more than one format is required. - * eg en_UK
- * - *See format
for format of the format
- * strings.
The user supplied method should have the following signature:
- * function validatePostcode(postcode:String):String
The method is passed the postcode to be validated and should - * return either: - *
The error string will be converted into a ValidationResult when - * doValidation is called by Flex as part of the normal validation - * process.
- * - * @langversion 3.0 - * @playerversion Flash 10.2 - * @productversion ApacheFlex 4.8 - */ - public var extraValidation:Function; + + /** + * @private + * The two letter country code used in some postcode formats + */ + private var _countryCode:String; + + /** + * @private + * An array of the postcode formats to check against. + */ + private var _formats:Array = []; + + /** + * Valid postcode format that postcodes will be compaired against. + * + *The format string consists of the letters C, N, A and spaces + * or hyphens: + *
Use for locales where more than one format is required. + * eg en_UK
+ * + *See format
for format of the format
+ * strings.
The user supplied method should have the following signature:
+ * function validatePostcode(postcode:String):String
The method is passed the postcode to be validated and should + * return either: + *
The error string will be converted into a ValidationResult when + * doValidation is called by Flex as part of the normal validation + * process.
+ * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @productversion ApacheFlex 4.8 + */ + public var extraValidation:Function; //-------------------------------------------------------------------------- // @@ -631,30 +585,28 @@ public class PostCodeValidator extends V * Storage for the invalidCharError property. */ private var _invalidCharError:String; - + /** * @private */ private var invalidCharErrorOverride:String; - [Inspectable(category="Errors", defaultValue="null")] + [Inspectable(category = "Errors", defaultValue = "null")] - /** + /** * Error message when the post code contains invalid characters. * * @default "The postcode code contains invalid characters." - * - * @langversion 3.0 - * @playerversion Flash 10.2 - * @productversion ApacheFlex 4.8 + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @productversion ApacheFlex 4.8 */ public function get invalidCharError():String { - if (invalidCharErrorOverride) - { - return invalidCharErrorOverride; - } - + if (invalidCharErrorOverride) + return invalidCharErrorOverride; + return _invalidCharError; } @@ -666,46 +618,41 @@ public class PostCodeValidator extends V invalidCharErrorOverride = value; if (!value) - { - _invalidCharError = resourceManager.getString("validators", - "invalidCharPostcodeError"); - } + _invalidCharError = resourceManager.getString("validators", "invalidCharPostcodeError"); } //---------------------------------- // wrongLengthError //---------------------------------- - + /** * @private * Storage for the wrongLengthError property. */ private var _wrongLengthError:String; - + /** * @private */ private var wrongLengthErrorOverride:String; - [Inspectable(category="Errors", defaultValue="null")] + [Inspectable(category = "Errors", defaultValue = "null")] - /** + /** * Error message for an invalid postcode. * * @default "The postcode is invalid." - * - * @langversion 3.0 - * @playerversion Flash 10.2 - * @productversion ApacheFlex 4.8 + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @productversion ApacheFlex 4.8 */ public function get wrongLengthError():String { - if (wrongLengthErrorOverride) - { - return wrongLengthErrorOverride; - } - + if (wrongLengthErrorOverride) + return wrongLengthErrorOverride; + return _wrongLengthError; } @@ -716,122 +663,110 @@ public class PostCodeValidator extends V { wrongLengthErrorOverride = value; - if (!value) - { - _wrongLengthError = resourceManager.getString("validators", - "wrongLengthPostcodeError"); - } - } - - //---------------------------------- - // wrongFormatError - //---------------------------------- - - /** - * @private - * Storage for the wrongFormatError property. - */ - private var _wrongFormatError:String; - - /** - * @private - */ - private var wrongFormatErrorOverride:String; - - [Inspectable(category="Errors", defaultValue="null")] - - /** - * Error message for an incorrectly formatted postcode. - * - * @default "The postcode code must be correctly formatted." - * - * @langversion 3.0 - * @playerversion Flash 10.2 - * @productversion ApacheFlex 4.8 - */ - public function get wrongFormatError():String - { - if (wrongFormatErrorOverride) - { - return wrongFormatErrorOverride; - } - - return _wrongFormatError; - } - - /** - * @private - */ - public function set wrongFormatError(value:String):void - { - wrongFormatErrorOverride = value; - - if (!value) - { - _wrongFormatError = resourceManager.getString("validators", - "wrongFormatPostcodeError"); - } - } - - //---------------------------------- - // incorrectFormatError - //---------------------------------- - - /** - * @private - * Storage for the incorrectFormatError property. - */ - private var _incorrectFormatError:String; - - /** - * @private - */ - private var incorrectFormatErrorOverride:String; - - [Inspectable(category="Errors", defaultValue="null")] - - /** - * Error message for an incorrect format string. - * - * @default "The postcode format string is incorrect" - * - * @langversion 3.0 - * @playerversion Flash 10.2 - * @productversion ApacheFlex 4.8 - */ - public function get incorrectFormatError():String - { - if (incorrectFormatErrorOverride) - { - return incorrectFormatErrorOverride; - } - - return _incorrectFormatError; - } - - /** - * @private - */ - public function set incorrectFormatError(value:String):void - { - incorrectFormatErrorOverride = value; - - if (!value) - { - _incorrectFormatError = resourceManager.getString("validators", - "incorrectFormatPostcodeError"); - } - } + if (!value) + _wrongLengthError = resourceManager.getString("validators", "wrongLengthPostcodeError"); + } + + //---------------------------------- + // wrongFormatError + //---------------------------------- + + /** + * @private + * Storage for the wrongFormatError property. + */ + private var _wrongFormatError:String; + + /** + * @private + */ + private var wrongFormatErrorOverride:String; + + [Inspectable(category = "Errors", defaultValue = "null")] + + /** + * Error message for an incorrectly formatted postcode. + * + * @default "The postcode code must be correctly formatted." + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @productversion ApacheFlex 4.8 + */ + public function get wrongFormatError():String + { + if (wrongFormatErrorOverride) + return wrongFormatErrorOverride; + + return _wrongFormatError; + } + + /** + * @private + */ + public function set wrongFormatError(value:String):void + { + wrongFormatErrorOverride = value; + + if (!value) + _wrongFormatError = resourceManager.getString("validators", "wrongFormatPostcodeError"); + } + + //---------------------------------- + // incorrectFormatError + //---------------------------------- + + /** + * @private + * Storage for the incorrectFormatError property. + */ + private var _incorrectFormatError:String; + + /** + * @private + */ + private var incorrectFormatErrorOverride:String; + + [Inspectable(category = "Errors", defaultValue = "null")] + + /** + * Error message for an incorrect format string. + * + * @default "The postcode format string is incorrect" + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @productversion ApacheFlex 4.8 + */ + public function get incorrectFormatError():String + { + if (incorrectFormatErrorOverride) + return incorrectFormatErrorOverride; + + return _incorrectFormatError; + } + + /** + * @private + */ + public function set incorrectFormatError(value:String):void + { + incorrectFormatErrorOverride = value; + + if (!value) + _incorrectFormatError = + resourceManager.getString("validators", "incorrectFormatPostcodeError"); + } //-------------------------------------------------------------------------- // // Overridden methods // //-------------------------------------------------------------------------- - + /** - * @private - * Changes error strings when the locale changes. + * @private + * Changes error strings when the locale changes. */ override protected function resourcesChanged():void { @@ -839,8 +774,8 @@ public class PostCodeValidator extends V invalidCharError = invalidCharErrorOverride; wrongLengthError = wrongLengthErrorOverride; - wrongFormatError = wrongFormatErrorOverride; - incorrectFormatError = incorrectFormatErrorOverride; + wrongFormatError = wrongFormatErrorOverride; + incorrectFormatError = incorrectFormatErrorOverride; } /** @@ -853,17 +788,17 @@ public class PostCodeValidator extends V * * @param value Object to validate. * - * @return An Array of ValidationResult objects, with one ValidationResult - * object for each field examined by the validator. - * - * @langversion 3.0 - * @playerversion Flash 10.2 - * @productversion ApacheFlex 4.8 + * @return An Array of ValidationResult objects, with one ValidationResult + * object for each field examined by the validator. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @productversion ApacheFlex 4.8 */ override protected function doValidation(value:Object):Array { var results:Array = super.doValidation(value); - + // Return if there are errors // or if the required property is set to false and length is 0. var val:String = value ? String(value) : ""; @@ -872,137 +807,138 @@ public class PostCodeValidator extends V else return PostCodeValidator.validatePostCode(this, val, null); } - - - //-------------------------------------------------------------------------- - // - // Methods - // - //-------------------------------------------------------------------------- - - /** - * @private - * Returns the region (usually country) from a locale string. - * If no loacle is provided the default locale is used. - * - * @param locale locale to obtain region from. - * @return Region string. - * - */ - protected function getRegion(locale:String):String { - var localeID:LocaleID; - - if (locale == null) - { - var tool:StringTools = new StringTools(LocaleID.DEFAULT); - localeID = new LocaleID(tool.actualLocaleIDName); - } - else - { - localeID = new LocaleID(locale); - } - - return localeID.getRegion(); - } - - /** - * Sets the suggested postcode formats for a givenlocale
.
- *
- * If no locale is suplied the default locale is used.
- * - *Currenly only a limited set of locales are supported.
- * - * @param locale Locale to obtain formats for. - * - * @return The suggested format (an array of strings) or an empty - * array if the locale is not supported. - * - * @langversion 3.0 - * @playerversion Flash 10.2 - * @productversion ApacheFlex 4.8 - */ - public function suggestFormat(locale:String = null):Array { - var region:String = getRegion(locale); - - switch (region) - { - case "AU": - case "DK": - case "NO": - formats = ["NNNN"]; - break; - case "BR": - formats = ["NNNNN-NNN"]; - break; - case "CN": - case "DE": - formats = ["NNNNNN"]; - break; - case "CA": - formats = ["ANA NAN"]; - break; - case "ES": - case "FI": - case "FR": - case "IT": - case "TW": - formats = ["NNNNN"]; - break; - case "GB": - formats = ["AN NAA", "ANN NAA", "AAN NAA", "ANA NAA", "AANN NAA", "AANA NAA"]; - break; - case "JP": - formats = ["NNNNNNN","NNN-NNNN","C NNNNNNN","C NNN-NNNN"]; - break; - case "KR": - formats = ["NNNNNN","NNN-NNN"]; - break; - case "NL": - formats = ["NNNN AA"]; - break; - case "RU": - formats = ["NNNNNN"]; - break; - case "SE": - formats = ["NNNNN","NNN NN"]; - break; - case "US": - formats = ["NNNNN", "NNNNN-NNNN"]; - break; - default: - formats = []; - break; - } - - return formats; - } - - /** - * Sets the suggested country code for for a givenlocale
.
- *
- * If no locale is suplied the default locale is used.
- * - *Currenly only a limited set of locales are supported.
- * - * @param Locale Locale to obtain country code for. - * - * @return The suggested code or an null string if the - * locale is not supported or has no country code. - * - * @langversion 3.0 - * @playerversion Flash 10.2 - * @productversion ApacheFlex 4.8 - */ - public function suggestCountryCode(locale:String = null):String { - var region:String = getRegion(locale); - - if (region == "JP") - { - countryCode = "ã"; - } - - return countryCode; - } + + + //-------------------------------------------------------------------------- + // + // Methods + // + //-------------------------------------------------------------------------- + + /** + * @private + * Returns the region (usually country) from a locale string. + * If no loacle is provided the default locale is used. + * + * @param locale locale to obtain region from. + * @return Region string. + * + */ + protected function getRegion(locale:String):String + { + var localeID:LocaleID; + + if (locale == null) + { + var tool:StringTools = new StringTools(LocaleID.DEFAULT); + localeID = new LocaleID(tool.actualLocaleIDName); + } + else + { + localeID = new LocaleID(locale); + } + + return localeID.getRegion(); + } + + /** + * Sets the suggested postcode formats for a givenlocale
.
+ *
+ * If no locale is suplied the default locale is used.
+ * + *Currenly only a limited set of locales are supported.
+ * + * @param locale Locale to obtain formats for. + * + * @return The suggested format (an array of strings) or an empty + * array if the locale is not supported. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @productversion ApacheFlex 4.8 + */ + public function suggestFormat(locale:String = null):Array + { + var region:String = getRegion(locale); + + switch (region) + { + case "AU": + case "DK": + case "NO": + formats = [ "NNNN" ]; + break; + case "BR": + formats = [ "NNNNN-NNN" ]; + break; + case "CN": + case "DE": + formats = [ "NNNNNN" ]; + break; + case "CA": + formats = [ "ANA NAN" ]; + break; + case "ES": + case "FI": + case "FR": + case "IT": + case "TW": + formats = [ "NNNNN" ]; + break; + case "GB": + formats = [ "AN NAA", "ANN NAA", "AAN NAA", "ANA NAA", "AANN NAA", "AANA NAA" ]; + break; + case "JP": + formats = [ "NNNNNNN", "NNN-NNNN", "C NNNNNNN", "C NNN-NNNN" ]; + break; + case "KR": + formats = [ "NNNNNN", "NNN-NNN" ]; + break; + case "NL": + formats = [ "NNNN AA" ]; + break; + case "RU": + formats = [ "NNNNNN" ]; + break; + case "SE": + formats = [ "NNNNN", "NNN NN" ]; + break; + case "US": + formats = [ "NNNNN", "NNNNN-NNNN" ]; + break; + default: + formats = []; + break; + } + + return formats; + } + + /** + * Sets the suggested country code for for a givenlocale
.
+ *
+ * If no locale is suplied the default locale is used.
+ * + *Currenly only a limited set of locales are supported.
+ * + * @param Locale Locale to obtain country code for. + * + * @return The suggested code or an null string if the + * locale is not supported or has no country code. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @productversion ApacheFlex 4.8 + */ + public function suggestCountryCode(locale:String = null):String + { + var region:String = getRegion(locale); + + if (region == "JP") + countryCode = "ã"; + + return countryCode; + } } }