Permalink
Browse files

Additionals: Add postalcodeBR method

Validates brazialian postal codes

Closes #1291
  • Loading branch information...
fernandopasse authored and jzaefferer committed Oct 10, 2014
1 parent 52debd4 commit cc6c4a4fd6a3390fe8b34b37c4eb0baaaa652372
Showing with 21 additions and 1 deletion.
  1. +11 −0 src/additional/postalcodeBR.js
  2. +2 −1 src/localization/messages_pt_BR.js
  3. +8 −0 test/methods.js
@@ -0,0 +1,11 @@
+/*
+* Valida CEPs do brasileiros:
+*
+* Formatos aceitos:
+* 99999-999
+* 99.999-999
+* 99999999
+*/
+$.validator.addMethod("postalcodeBR", function(cep_value, element) {
+ return this.optional(element) || /^\d{2}.\d{3}-\d{3}?$|^\d{5}-?\d{3}?$/.test( cep_value );
+}, "Informe um CEP válido.");
@@ -23,5 +23,6 @@ $.extend($.validator.messages, {
min: $.validator.format("Por favor, forneça um valor maior ou igual a {0}."),
nifES: "Por favor, forneça um NIF válido.",
nieES: "Por favor, forneça um NIE válido.",
- cifEE: "Por favor, forneça um CIF válido."
+ cifEE: "Por favor, forneça um CIF válido.",
+ postalcodeBR: "Por favor, forneça um CEP válido."
});
View
@@ -1261,4 +1261,12 @@ test("stateUS", function() {
ok(!method( "mp", { caseSensitive: false, includeTerritories: false } ), "US territories not allowed" );
});
+test("postalcodeBR", function() {
+ var method = methodTest("postalcodeBR");
+ ok( method( "99999-999"), "Valid BR Postal Code");
+ ok( method( "99999999"), "Valid BR Postal Code");
+ ok( method( "99.999-999"), "Valid BR Postal Code");
+ ok( !method( "99.999999"), "Invalid BR Postal Code");
+});
+
})(jQuery);

0 comments on commit cc6c4a4

Please sign in to comment.