incubator-flex-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmcl...@apache.org
Subject svn commit: r1296716 - /incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as
Date Sat, 03 Mar 2012 22:23:38 GMT
Author: jmclean
Date: Sat Mar  3 22:23:38 2012
New Revision: 1296716

URL: http://svn.apache.org/viewvc?rev=1296716&view=rev
Log:
cleaned up error checking logic to return the least number of errors in a list of formats

Modified:
    incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as

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=1296716&r1=1296715&r2=1296716&view=diff
==============================================================================
--- incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as (original)
+++ incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as Sat
Mar  3 22:23:38 2012
@@ -83,32 +83,34 @@ public class PostCodeValidator extends V
 
         var postCode:String = String(value);
         var length:int = postCode.length;
+		var formatLength:int;
 		var noformats:int = _formats.length;
 		var validLetters:String = "CAN";
 		var spacers:String = " -";
 		
+		var errors:Array = [];
 		var wrongLength:Boolean;
 		var invalidChar:Boolean;
 		var invalidFormat:Boolean;
 		
-		var allInvalidFormat:Boolean;
-		
-		var wrongLengthAdded:Boolean;
-		var invalidCharAdded:Boolean;
-		var invalidFormatAdded:Boolean;
-		
-		var digit:int;
+		var countryDigit:int;
 		
 		for (var f:int = 0; f < noformats; f++)
 		{	
+			countryDigit = 0;
+			invalidChar = false;
 			invalidFormat = false;
-			digit = 0;
+			formatLength = _formats[f].length;
 			
 			for (var i:int = 0; i < length; i++)
 			{
 				var char:String = postCode.charAt(i);
 				var formatChar:String = _formats[f].charAt(i);
 				
+				if (i >= formatLength) {
+					break;
+				}
+				
 				if (DECIMAL_DIGITS.indexOf(char) == -1
 					&& ROMAN_LETTERS.indexOf(char) == -1 && spacers.indexOf(char) == -1)
 				{
@@ -124,45 +126,48 @@ public class PostCodeValidator extends V
 				}
 				else if (formatChar == "C")
 				{
-					if (digit >= 2 || !_countryCode || char != _countryCode.charAt(digit))
+					if (countryDigit >= 2 || !_countryCode || char != _countryCode.charAt(countryDigit))
 					{
 						invalidFormat = true;
 					}
-					digit++;
+					countryDigit++;
 				}
 				else if (spacers.indexOf(formatChar) >= 0 && formatChar != char) {
 					invalidFormat = true;
 				}
 			}
 			
+			wrongLength = (length != formatLength);
+			
 			// found a match so return without error
-			if (!invalidFormat && !invalidChar && (length == _formats[f].length))
{
+			if (!invalidFormat && !invalidChar && !wrongLength) {
 				return [];
 			}
 			
-			wrongLength = wrongLength || (length != _formats[f].length);
-			allInvalidFormat = allInvalidFormat || invalidFormat;
+			errors.push({invalidFormat:invalidFormat, invalidChar:invalidChar,
+				wrongLength:wrongLength, count:invalidFormat + invalidChar + wrongLength})
 		}
 		
-		if (invalidChar && !invalidCharAdded)
+		// return result with least number of errors
+		// TODO return/remember closest format?
+		errors.sortOn("count", Array.NUMERIC);
+		
+		if (errors[0].invalidChar)
 		{
-			invalidCharAdded = true;
 			results.push(new ValidationResult(
 				true, baseField, "invalidChar",
 				validator.invalidCharError));
 		}
         
-        if (wrongLength && !wrongLengthAdded)
+        if (errors[0].wrongLength)
         {
-			wrongLengthAdded = true;
             results.push(new ValidationResult(
                 true, baseField, "wrongLength",
                 validator.wrongLengthError));
         }
         
-		if (allInvalidFormat && !invalidFormatAdded)
+		if (errors[0].invalidFormat)
 		{
-			invalidFormatAdded = true;
       	 	results.push(new ValidationResult(
                 true, baseField, "wrongFormat",
                 validator.wrongFormatError));
@@ -264,7 +269,7 @@ public class PostCodeValidator extends V
 	 */
 	public function set countryCode(value:String):void
 	{
-		if (value && value.length == 2)
+		if (value == null || value && value.length == 2)
 		{
 			_countryCode = value;
 		}



Mime
View raw message