incubator-flex-commits mailing list archives

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

URL: http://svn.apache.org/viewvc?rev=1297316&view=rev
Log:
added RTE checks and tests

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=1297316&r1=1297315&r2=1297316&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 01:29:54 2012
@@ -28,6 +28,18 @@ import mx.resources.ResourceManager;
 /**
  *  The PostCodeValidator class validates that a String
  *  has the correct length and format for a post code.
+ * 
+ *  Postcode format consists of CC,N,A and spaces or hyphens
+ *  CC is country code (required for some postcodes)
+ *	N is a number 0-9
+ *  A is a letter A-Z or a-z
+ * 
+ *  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
+ *  followed by four digits.
+ * 
+ *  More than one format can be specified by setting the <code>formats</code>
+ *  property to an array of formats.
  *  
  *  @mxml
  *
@@ -37,7 +49,8 @@ import mx.resources.ResourceManager;
  *  
  *  <pre>
  *  &lt;mx:PostCodeValidator
- *    format="NNNN"
+ *    format="NNNNN"
+ *    formats="['NNNNN', 'NNNNN-NNNN']"
  *    invalidCharError="The post code contains invalid characters." 
  *    wrongLengthError="The post code is the wrong length" 
  *  /&gt;
@@ -86,9 +99,9 @@ public class PostCodeValidator extends V
     {
         var results:Array = [];
 
-        var length:int = postCode.length;
+        var length:int;
 		var formatLength:int;
-		var noformats:int = validator.formats.length;
+		var noformats:int;
 		var spacers:String = " -";
 		
 		var errors:Array = [];
@@ -98,6 +111,11 @@ public class PostCodeValidator extends V
 		
 		var countryDigit:int;
 		
+		if (validator)
+		{
+			noformats = validator.formats.length
+		}
+		
 		for (var f:int = 0; f < noformats; f++)
 		{	
 			countryDigit = 0;
@@ -105,6 +123,10 @@ public class PostCodeValidator extends V
 			invalidFormat = false;
 			formatLength = validator.formats[f].length;
 			
+			if (postCode) {
+				length = postCode.length;
+			}
+			
 			for (var i:int = 0; i < length; i++)
 			{
 				var char:String = postCode.charAt(i);
@@ -158,25 +180,27 @@ public class PostCodeValidator extends V
 		// TODO return/remember closest format or place in error string?
 		errors.sortOn("count", Array.NUMERIC);
 		
-		if (errors[0].invalidChar)
-		{
-			results.push(new ValidationResult(
-				true, baseField, "invalidChar",
-				validator.invalidCharError));
-		}
-        
-        if (errors[0].wrongLength)
-        {
-            results.push(new ValidationResult(
-                true, baseField, "wrongLength",
-                validator.wrongLengthError));
-        }
-        
-		if (errors[0].invalidFormat)
-		{
-      	 	results.push(new ValidationResult(
-                true, baseField, "wrongFormat",
-                validator.wrongFormatError));
+		if (errors && errors.length > 0) {
+			if (errors[0].invalidChar)
+			{
+				results.push(new ValidationResult(
+					true, baseField, "invalidChar",
+					validator.invalidCharError));
+			}
+	        
+	        if (errors[0].wrongLength)
+	        {
+	            results.push(new ValidationResult(
+	                true, baseField, "wrongLength",
+	                validator.wrongLengthError));
+	        }
+	        
+			if (errors[0].invalidFormat)
+			{
+	      	 	results.push(new ValidationResult(
+	                true, baseField, "wrongFormat",
+	                validator.wrongFormatError));
+			}
 		}
 
         return results;
@@ -220,8 +244,8 @@ public class PostCodeValidator extends V
 	
 	/** 
 	 *  Format of postcode
-	 *  Format constist of CC,N,A and space
-	 *  CC is country code (rquired for some postcodes)
+	 *  Format consists of CC,N,A and spaces or hyphens
+	 *  CC is country code (required for some postcodes)
 	 *	N is a number 0-9
 	 *  A is a letter A-Z or a-z
 	 *

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=1297316&r1=1297315&r2=1297316&view=diff
==============================================================================
--- incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeValidatorTests.as (original)
+++ incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeValidatorTests.as Tue Mar
 6 01:29:54 2012
@@ -30,6 +30,44 @@ package tests
 		}
 		
 		[Test]
+		public function noRTEs():void
+		{
+			var results:Array;
+			
+			results = PostCodeValidator.validatePostCode(null, null, null);
+			assertTrue("No errors", results.length == 0);
+			results = PostCodeValidator.validatePostCode(null, "", null);
+			assertTrue("No errors", results.length == 0);
+			results = PostCodeValidator.validatePostCode(null, "2000", null);
+			assertTrue("No errors", results.length == 0);
+			
+			results = PostCodeValidator.validatePostCode(validator, null, null);
+			assertTrue("No errors", results.length == 0);
+			results = PostCodeValidator.validatePostCode(validator, "", null);
+			assertTrue("No errors", results.length == 0);
+			results = PostCodeValidator.validatePostCode(validator, "2000", null);
+			assertTrue("No errors", results.length == 0);
+			
+			validator.format = "NNNN";
+			
+			results = PostCodeValidator.validatePostCode(null, null, null);
+			assertTrue("No errors", results.length == 0);
+			results = PostCodeValidator.validatePostCode(null, "", null);
+			assertTrue("No errors", results.length == 0);
+			results = PostCodeValidator.validatePostCode(null, "2000", null);
+			assertTrue("No errors", results.length == 0);
+			
+			results = PostCodeValidator.validatePostCode(validator, null, null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			wrongLengthError(results);
+			results = PostCodeValidator.validatePostCode(validator, "", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			wrongLengthError(results);
+			results = PostCodeValidator.validatePostCode(validator, "2000", null);
+			assertTrue("Valid Postcode", results.length == 0);
+		}
+		
+		[Test]
 		public function setFormats():void {
 			validator.format = "NNNN";
 			assertTrue("Format is correct", validator.format = "NNNN");
@@ -326,6 +364,19 @@ package tests
 		}
 		
 		[Test]
+		public function japanesePostcodes():void {
+			var results:Array;
+			var valid:Array = ["100-0006", "600-8216", "282-0004"];
+			
+			validator.format = "NNN-NNNN";
+			
+			for each (var postcode:String in valid) {
+				results = PostCodeValidator.validatePostCode(validator, postcode, null);
+				assertTrue("Valid Postcode", results.length == 0);
+			}	
+		}
+		
+		[Test]
 		public function multipleFormats():void {
 			var validator1:PostCodeValidator = new PostCodeValidator();
 			var validator2:PostCodeValidator = new PostCodeValidator();



Mime
View raw message