Permalink
Browse files

Additionals: Brazilian CPF number

Closes #1310
  • Loading branch information...
gorork authored and jzaefferer committed Oct 24, 2014
1 parent 460ac6b commit 89034eb4d479b208fdf63c787a78b1655b895d33
Showing with 72 additions and 1 deletion.
  1. +57 −0 src/additional/cpfBR.js
  2. +2 −1 src/localization/messages_pt_BR.js
  3. +13 −0 test/methods.js
View
@@ -0,0 +1,57 @@
+/*
+ * Brazillian CPF number (Cadastrado de Pessoas Físicas) is the equivalent of a Brazilian tax registration number.
+ * CPF numbers have 11 digits in total: 9 numbers followed by 2 check numbers that are being used for validation.
+ */
+$.validator.addMethod("cpfBR", function(value) {
+ // Removing special characters from value
+ value = value.replace(/([~!@#$%^&*()_+=`{}\[\]\-|\\:;'<>,.\/? ])+/g, "");
+
+ // Checking value to have 11 digits only
+ if (value.length !== 11) {
+ return false;
+ }
+
+ var sum = 0,
+ firstCN, secondCN, checkResult, i;
+
+ firstCN = parseInt(value.substring(9, 10), 10);
+ secondCN = parseInt(value.substring(10, 11), 10);
+
+ checkResult = function(sum, cn) {
+ var result = (sum * 10) % 11;
+ if ((result === 10) || (result === 11)) {result = 0;}
+ return (result === cn);
+ };
+
+ // Checking for dump data
+ if (value === "" ||
+ value === "00000000000" ||
+ value === "11111111111" ||
+ value === "22222222222" ||
+ value === "33333333333" ||
+ value === "44444444444" ||
+ value === "55555555555" ||
+ value === "66666666666" ||
+ value === "77777777777" ||
+ value === "88888888888" ||
+ value === "99999999999"
+ ) {
+ return false;
+ }
+
+ // Step 1 - using first Check Number:
+ for ( i = 1; i <= 9; i++ ) {
+ sum = sum + parseInt(value.substring(i - 1, i), 10) * (11 - i);
+ }
+
+ // If first Check Number (CN) is valid, move to Step 2 - using second Check Number:
+ if ( checkResult(sum, firstCN) ) {
+ sum = 0;
+ for ( i = 1; i <= 10; i++ ) {
+ sum = sum + parseInt(value.substring(i - 1, i), 10) * (12 - i);
+ }
+ return checkResult(sum, secondCN);
+ }
+ return false;
+
+}, "Please specify a valid CPF number");
@@ -24,5 +24,6 @@ $.extend($.validator.messages, {
nifES: "Por favor, forne&ccedil;a um NIF v&aacute;lido.",
nieES: "Por favor, forne&ccedil;a um NIE v&aacute;lido.",
cifEE: "Por favor, forne&ccedil;a um CIF v&aacute;lido.",
- postalcodeBR: "Por favor, forne&ccedil;a um CEP v&aacute;lido."
+ postalcodeBR: "Por favor, forne&ccedil;a um CEP v&aacute;lido.",
+ cpfBR: "Por favor, forne&ccedil;a um CPF v&aacute;lido."
});
View
@@ -1269,4 +1269,17 @@ test("postalcodeBR", function() {
ok( !method( "99.999999"), "Invalid BR Postal Code");
});
+test("cpfBR", function() {
+ var method = methodTest("cpfBR");
+ ok( method( "11144477735"), "Valid CPF Number");
+ ok( method( "263.946.533-30"), "Valid CPF Number");
+ ok( method( "325 861 044 47"), "Valid CPF Number");
+ ok( method( "859-684-732-40"), "Valid CPF Number");
+ ok( !method( "99999999999"), "Invalid CPF Number: dump data");
+ ok( !method( "1114447773"), "Invalid CPF Number: < 11 digits");
+ ok( !method( "111444777355"), "Invalid CPF Number: > 11 digits");
+ ok( !method( "11144477715"), "Invalid CPF Number: 1st check number failed");
+ ok( !method( "11144477737"), "Invalid CPF Number: 2nd check number failed");
+});
+
})(jQuery);

0 comments on commit 89034eb

Please sign in to comment.