Skip to content

Commit 278d3f3

Browse files
mlynchjzaefferer
authored andcommitted
Added US zip code support and test. Fixes jquery-validation#90
1 parent c41f2f6 commit 278d3f3

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

additional-methods.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ jQuery.validator.addMethod("ziprange", function(value, element) {
5555
return this.optional(element) || /^90[2-5]\d\{2\}-\d{4}$/.test(value);
5656
}, "Your ZIP-code must be in the range 902xx-xxxx to 905-xx-xxxx");
5757

58+
jQuery.validator.addMethod("zipcodeUS", function(value, element) {
59+
return this.optional(element) || /\d{5}-\d{4}$|^\d{5}$/.test(value)
60+
}, "The specified US ZIP Code is invalid");
61+
5862
jQuery.validator.addMethod("integer", function(value, element) {
5963
return this.optional(element) || /^-?\d+$/.test(value);
6064
}, "A positive or negative non-decimal number please");

test/methods.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,4 +725,14 @@ test('skip_or_fill_minimum', function() {
725725
fillFormWithValuesAndExpect('#productInfo', [123, 'widget', 'red'], true);
726726
});
727727

728+
test("zipcodeUS", function() {
729+
var method = methodTest("zipcodeUS");
730+
ok( method( "12345" ), "Valid zip" );
731+
ok( method( "12345-2345" ), "Valid zip" );
732+
ok(!method( "1" ), "Invalid zip" );
733+
ok(!method( "1234" ), "Invalid zip" );
734+
ok(!method( "123-23" ), "Invalid zip" );
735+
ok(!method( "12345-43" ), "Invalid zip" );
736+
});
737+
728738
})(jQuery);

0 commit comments

Comments
 (0)