Return-Path: X-Original-To: apmail-incubator-flex-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-flex-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D475D9902 for ; Mon, 12 Mar 2012 12:21:40 +0000 (UTC) Received: (qmail 20100 invoked by uid 500); 12 Mar 2012 12:21:40 -0000 Delivered-To: apmail-incubator-flex-commits-archive@incubator.apache.org Received: (qmail 20016 invoked by uid 500); 12 Mar 2012 12:21:40 -0000 Mailing-List: contact flex-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: flex-dev@incubator.apache.org Delivered-To: mailing list flex-commits@incubator.apache.org Received: (qmail 20009 invoked by uid 99); 12 Mar 2012 12:21:40 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 12 Mar 2012 12:21:40 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 12 Mar 2012 12:21:36 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id A301523889E1; Mon, 12 Mar 2012 12:21:16 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: svn commit: r1299646 - in /incubator/flex/whiteboard/jmclean/validators/src/mx: formatters/PostCodeFormatter.as validators/PostCodeValidator.as Date: Mon, 12 Mar 2012 12:21:16 -0000 To: flex-commits@incubator.apache.org From: jmclean@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120312122116.A301523889E1@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: jmclean Date: Mon Mar 12 12:21:16 2012 New Revision: 1299646 URL: http://svn.apache.org/viewvc?rev=1299646&view=rev Log: Re formatted to match Adobe style via Flex Formatter (90% tabs to spaces and braces placement) Modified: incubator/flex/whiteboard/jmclean/validators/src/mx/formatters/PostCodeFormatter.as incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as Modified: incubator/flex/whiteboard/jmclean/validators/src/mx/formatters/PostCodeFormatter.as URL: http://svn.apache.org/viewvc/incubator/flex/whiteboard/jmclean/validators/src/mx/formatters/PostCodeFormatter.as?rev=1299646&r1=1299645&r2=1299646&view=diff ============================================================================== --- incubator/flex/whiteboard/jmclean/validators/src/mx/formatters/PostCodeFormatter.as (original) +++ incubator/flex/whiteboard/jmclean/validators/src/mx/formatters/PostCodeFormatter.as Mon Mar 12 12:21:16 2012 @@ -24,7 +24,7 @@ import mx.events.ValidationResultEvent; import mx.managers.ISystemManager; import mx.managers.SystemManager; import mx.validators.PostCodeValidator; -import mx.validators.ValidationResult; +import mx.validators.ValidationResult; [ResourceBundle("formatters")] @@ -32,27 +32,27 @@ import mx.validators.ValidationResult; * The PostCodeFormatter class formats a valid postcode * based on a user set formatString or * formats property. - * + * *

Postcode formats consists of the letters C, N, A and spaces or hyphens *

    *
  • CC or C is the country code (required for some postcodes).
  • *
  • N is a number 0-9.
  • *
  • A is a letter A-Z or a-z,
  • *

- * + * *

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.

- * + * * @mxml - * + * *

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: - *

    - *
  • CC or C is country code (required for some postcodes).
  • - *
  • N is a number 0-9.
  • - *
  • A is a letter A-Z or a-z.
  • - *

- * - * @default null - * - * @langversion 3.0 - * @playerversion Flash 10.2 - * @productversion ApacheFlex 4.8 - */ - public function get formatString():String - { - if (_formats && _formats.length == 1) - { - return _formats[0]; - } - - return null; - } - - /** - * @private - */ - public function set formatString(value:String):void - { - if (value != null) - { - _formats = [value]; - } - else - { - _formats = []; - } - } - - /** - * An array of format strings to format the postcode in. - * - *

Use for locales where more than one format is required. - * eg en_UK

- * - *

See formatString for format of the format - * strings.

- * - * @default [] - * - * @langversion 3.0 - * @playerversion Flash 10.2 - * @productversion ApacheFlex 4.8 - */ - public function get formats():Array - { - return _formats; - } - - /** - * @private - */ - public function set formats(value:Array):void - { - _formats = value; - } - - //-------------------------------------------------------------------------- - // - // Overidden methods - // - //-------------------------------------------------------------------------- - - /** - * Formats the value by using the format set in 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.

- * - * @param value Value to format. - * - * @return Formatted String. Empty if an error occurs. A description - * of the error condition is written to the 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: + *

    + *
  • CC or C is country code (required for some postcodes).
  • + *
  • N is a number 0-9.
  • + *
  • A is a letter A-Z or a-z.
  • + *

+ * + * @default null + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @productversion ApacheFlex 4.8 + */ + public function get formatString():String + { + if (_formats && _formats.length == 1) + return _formats[0]; + + return null; + } + + /** + * @private + */ + public function set formatString(value:String):void + { + if (value != null) + _formats = [ value ]; + else + _formats = []; + } + + /** + * An array of format strings to format the postcode in. + * + *

Use for locales where more than one format is required. + * eg en_UK

+ * + *

See formatString for format of the format + * strings.

+ * + * @default [] + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @productversion ApacheFlex 4.8 + */ + public function get formats():Array + { + return _formats; + } + + /** + * @private + */ + public function set formats(value:Array):void + { + _formats = value; + } + + //-------------------------------------------------------------------------- + // + // Overidden methods + // + //-------------------------------------------------------------------------- + + /** + * Formats the value by using the format set in 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.

+ * + * @param value Value to format. + * + * @return Formatted String. Empty if an error occurs. A description + * of the error condition is written to the 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 *

    *
  • CC or C is the country code (required for some postcodes).
  • *
  • N is a number 0-9.
  • *
  • A is a letter A-Z or a-z.
  • *

- * + * *

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.

- * + * * @mxml * *

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: - *

    - *
  • CC or C is country code (required for some postcodes).
  • - *
  • N is a number 0-9.
  • - *
  • A is a letter A-Z or a-z.
  • - *

- * - * @default null - * - * @langversion 3.0 - * @playerversion Flash 10.2 - * @productversion ApacheFlex 4.8 - */ - public function get format():String - { - if (_formats && _formats.length == 1) - { - return _formats[0]; - } - - return null; - } - - /** - * @private - */ - public function set format(value:String):void - { - if (value != null) - { - _formats = [value]; - } - else - { - _formats = [] - } - } - - /** - * Optional 1 or 2 letter country code in postcode format - * - * @default null - * - * @langversion 3.0 - * @playerversion Flash 10.2 - * @productversion ApacheFlex 4.8 - */ - public function get countryCode():String - { - return _countryCode; - } - - /** - * @private - */ - public function set countryCode(value:String):void - { - // Length is usually 2 character but can use 〒 in Japan - if (value == null || value && value.length <= 2) - { - _countryCode = value; - } - } - - /** - * An array of valid format strings to compare postcodes against. - * - *

Use for locales where more than one format is required. - * eg en_UK

- * - *

See format for format of the format - * strings.

- * - * @default [] - * - * @langversion 3.0 - * @playerversion Flash 10.2 - * @productversion ApacheFlex 4.8 - */ - public function get formats():Array - { - return _formats; - } - - /** - * @private - */ - public function set formats(value:Array):void - { - _formats = value; - } - - /** - * A user supplied method that performs further validation on a postcode. - * - *

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: - *

    - *
  1. A null string indicating the postcode is valid.
  2. - *
  3. A non empty string describing why the postcode is invalid.
  4. - *

- * - *

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: + *

    + *
  • CC or C is country code (required for some postcodes).
  • + *
  • N is a number 0-9.
  • + *
  • A is a letter A-Z or a-z.
  • + *

+ * + * @default null + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @productversion ApacheFlex 4.8 + */ + public function get format():String + { + if (_formats && _formats.length == 1) + return _formats[0]; + + return null; + } + + /** + * @private + */ + public function set format(value:String):void + { + if (value != null) + _formats = [ value ]; + else + _formats = []; + } + + /** + * Optional 1 or 2 letter country code in postcode format + * + * @default null + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @productversion ApacheFlex 4.8 + */ + public function get countryCode():String + { + return _countryCode; + } + + /** + * @private + */ + public function set countryCode(value:String):void + { + // Length is usually 2 character but can use 〒 in Japan + if (value == null || value && value.length <= 2) + _countryCode = value; + } + + /** + * An array of valid format strings to compare postcodes against. + * + *

Use for locales where more than one format is required. + * eg en_UK

+ * + *

See format for format of the format + * strings.

+ * + * @default [] + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @productversion ApacheFlex 4.8 + */ + public function get formats():Array + { + return _formats; + } + + /** + * @private + */ + public function set formats(value:Array):void + { + _formats = value; + } + + /** + * A user supplied method that performs further validation on a postcode. + * + *

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: + *

    + *
  1. A null string indicating the postcode is valid.
  2. + *
  3. A non empty string describing why the postcode is invalid.
  4. + *

+ * + *

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 given locale. - * - *

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 given locale. - * - *

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 given locale. + * + *

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 given locale. + * + *

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; + } } }