incubator-flex-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmcl...@apache.org
Subject svn commit: r1297381 - in /incubator/flex/whiteboard/jmclean/validators/src: mx/validators/PostCodeValidator.as tests/PostCodeValidatorTests.as
Date Tue, 06 Mar 2012 08:36:32 GMT
Author: jmclean
Date: Tue Mar  6 08:36:32 2012
New Revision: 1297381

URL: http://svn.apache.org/viewvc?rev=1297381&view=rev
Log:
added user defined property extraValidation to perform user defined postcode validation

Modified:
    incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as
    incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeValidatorTests.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=1297381&r1=1297380&r2=1297381&view=diff
==============================================================================
--- incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as (original)
+++ incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as Tue
Mar  6 08:36:32 2012
@@ -165,9 +165,10 @@ public class PostCodeValidator extends V
 			
 			wrongLength = (length != formatLength);
 			
-			// found a match so return without error
+			// found a match so no need to check further
 			if (!invalidFormat && !invalidChar && !wrongLength) {
-				return [];
+				errors = null;
+				break;
 			}
 			
 			// We want invalid char and invalid format errors show in preference
@@ -176,11 +177,21 @@ public class PostCodeValidator extends V
 				count:Number(invalidFormat) + Number(invalidChar) + Number(wrongLength)*1.5})
 		}
 		
-		// return result with least number of errors
-		// TODO return/remember closest format or place in error string?
-		errors.sortOn("count", Array.NUMERIC);
+		if (validator && validator.extraValidation)
+		{
+			var extraError:String = validator.extraValidation(postCode);
+			
+			if (extraError) {
+				results.push(new ValidationResult(
+					true, baseField, "wrongFormat", extraError));
+			}
+		}
 		
 		if (errors && errors.length > 0) {
+			// return result with least number of errors
+			// TODO return/remember closest format or place in error string?
+			errors.sortOn("count", Array.NUMERIC);
+			
 			if (errors[0].invalidChar)
 			{
 				results.push(new ValidationResult(
@@ -202,7 +213,7 @@ public class PostCodeValidator extends V
 	                validator.wrongFormatError));
 			}
 		}
-
+		
         return results;
     }
     
@@ -330,6 +341,22 @@ public class PostCodeValidator extends V
 	{
 		_formats = value;
 	}
+	
+	/** 
+	 *  extraValidation
+	 * 
+	 * A user supplied method that perform further validation on a postcode.
+	 * 
+	 * The user supplied method should take as a parameter a postcode (String)
+	 * and if the postcode is not valid return an error (String).
+	 *
+	 *  @default null
+	 *  
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @productversion ApacheFlex 4.8
+	 */
+	public var extraValidation:Function;
 
     //--------------------------------------------------------------------------
     //

Modified: incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeValidatorTests.as
URL: http://svn.apache.org/viewvc/incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeValidatorTests.as?rev=1297381&r1=1297380&r2=1297381&view=diff
==============================================================================
--- incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeValidatorTests.as (original)
+++ incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeValidatorTests.as Tue Mar
 6 08:36:32 2012
@@ -389,8 +389,79 @@ package tests
 			assertTrue("Valid Postcode", results.length == 0);
 			results = PostCodeValidator.validatePostCode(validator2, "123456", null);
 			assertTrue("Valid Postcode", results.length == 0);
+		}
+		
+		[Test]
+		public function checkCanadianLetters():void {
+			var results:Array;
+			
+			validator.format = "ANA NAN";
+			validator.extraValidation = canadianLetters;
+			
+			results = PostCodeValidator.validatePostCode(validator, "K0K 2T0", null);
+			assertTrue("Valid Postcode", results.length == 0);
+			
+			results = PostCodeValidator.validatePostCode(validator, "K0D 2I0", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			invalidFormatError(results); //TODO mark as user user?
+		}
+		
+		public function canadianLetters(postCode:String):String {
+			var notUsed:String = "DFIOQU";
+			var length:int = notUsed.length;
+			var char:String;
+			
+			for (var i:int = 0; i < length; i++) {
+				char = notUsed.charAt(i);
+				
+				if (postCode.indexOf(char) >= 0) {
+					return "Postcode containts an invalid character D,F,I,O,Q or U";
+				}
+			}
+			
+			return null;
+		}
 
+		[Test]
+		public function checkAustraliaPOBoxes():void {
+			var results:Array;
+			
+			validator.format = "NNNN";
+			validator.extraValidation = australiaPOBox;
+			
+			results = PostCodeValidator.validatePostCode(validator, "2010", null);
+			assertTrue("Valid Postcode", results.length == 0);
+			
+			results = PostCodeValidator.validatePostCode(validator, "0250", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			invalidFormatError(results); //TODO mark as user user?
+			
+			results = PostCodeValidator.validatePostCode(validator, "5820", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			invalidFormatError(results); //TODO mark as user user?
 		}
 		
+		public function australiaPOBox(postCode:String):String {
+			var postCodeNum:int = int(postCode);
+			var ranges:Array = [
+				{low:1000, high:1999},
+				{low:200, high:299},
+				{low:8000, high:8999},
+				{low:9000, high:9999},
+				{low:5800, high:5999},
+				{low:6800, high:6999},
+				{low:7800, high:7999}, 
+				{low:900, high:999}];
+			var length:int = ranges.length;
+			
+			for (var i:int = 0; i < length; i++) {	
+				if (postCodeNum >= ranges[i].low && postCodeNum <= ranges[i].high) {
+					return "Postcode is a PO Box number";
+				}
+			}
+
+			return null;
+		}
+
 	}
 }
\ No newline at end of file



Mime
View raw message