Opened 6 years ago

Closed 5 years ago

Last modified 5 years ago

#17367 closed defect (fixed)

[patch][cla] dojox.validate.br isValidCpf error

Reported by: raupp Owned by: dylan
Priority: high Milestone: 1.7.6
Component: Dojox Version: 1.9.1
Keywords: validate, i18n Cc:
Blocked By: Blocking:

Description

The validator of CPF on dojox.validate.br have a error. The validator return false when a valid cpf like: 066.487.679-01 is inserted. I rewrite the isValidCpf:

br.isValidCpf = function(/*String*/value){

summary: Validates a CPF number value: String The CPF number in #########-## or ###########, format if(!lang.isString(value)){

if(!value){

return false;

} value = value + ""; while(value.length < 11){

value = "0" + value;

}

} var flags = {

format: [

"###.###.###-##", "#########-##", "###########"

]

}; if(validate.isNumberFormat(value, flags)){

Matched the initial test, so break this down into the parts to be validated. value = value.replace("-", "").replace(/\./g, ""); var cpf = []; var dv = []; var i, j, tmp;

Check for obvious bad combos all 0s to all 9's. for(i = 0; i < 10; i++){

tmp = ""; for(j = 0; j < value.length; j++){

tmp += "" + i;

} if(value === tmp){

return false;

}

}

Split out the DV from the main number. for(i = 0; i < 9; i++){

cpf.push(parseInt(value.charAt(i), 10));

} for(i = 9; i < 12; i++){

dv.push(parseInt(value.charAt(i), 10));

}

var base = [10,9,8,7,6,5,4,3,2]; var sum = 0; for(i = 0; i < cpf.length; i++){

sum += cpf[i] * base[i];

} var dv0 = sum % 11; dv0 = dv0<2?0:11-dv0;

if(dv0 == dv[0]){

Still seems valid, keep going. sum = 0; base = [11,10,9,8,7,6,5,4,3,2]; cpf.push(dv0); for(i = 0; i < cpf.length; i++){

sum += cpf[i] * base[i];

} var dv1 = sum % 11;

dv1 = dv1<2?0:11-dv1;

if(dv1 === dv[1]){

Whew, looks valid. return true;

}

}

} return false;

};

Attachments (3)

br.js (5.7 KB) - added by raupp 6 years ago.
modified validate
br.2.js (5.8 KB) - added by avrahamr 6 years ago.
Fixed version of br.js
br.3.js (7.1 KB) - added by avrahamr 6 years ago.
tests/br.js - Added the problematic CPF and CNPJ, with my fix it passes

Download all attachments as: .zip

Change History (14)

Changed 6 years ago by raupp

Attachment: br.js added

modified validate

comment:1 Changed 6 years ago by bill

Component: DojoX FormDojox
Owner: set to Jared Jurkiewicz
Status: newassigned

Can you open a pull request for these changes, including updating the existing test cases?

comment:2 Changed 6 years ago by raupp

I tried do this, following the CONTRIBUTING.md, I hope have done it right. The hash generated from github is 90636e4681bcc0e1040b5c9eacc890354d8921e4 If there's some problem, please report

Changed 6 years ago by avrahamr

Attachment: br.2.js added

Fixed version of br.js

Changed 6 years ago by avrahamr

Attachment: br.3.js added

tests/br.js - Added the problematic CPF and CNPJ, with my fix it passes

comment:3 Changed 6 years ago by avrahamr

My fix is fairly simple, I've added a private function:

var calcDv = function(sum) {
  var dv = sum % 11;
  if (dv > 9) dv = 0;
  return dv;
}

Then replaced all instances of

var dv0 = sum % 11;

With

var dv0 = calcDv(sum);

Based my fix on this http://snipplr.com/view/60227/cpf-and-cnpj-validation-classes-using-the-brazilian-government-official-algorithms/

comment:4 Changed 6 years ago by avrahamr

comment:5 Changed 5 years ago by dylan

Keywords: validate i18n added
Priority: undecidedhigh
Summary: dojox.validate.br isValidCpf error[patch] dojox.validate.br isValidCpf error

Hi avrahamr, do you have a CLA on file? See the contributing guidelines for more details: https://github.com/dojo/dojo/blob/master/CONTRIBUTING.md

comment:6 Changed 5 years ago by dylan

Milestone: tbd1.10
Owner: changed from Jared Jurkiewicz to dylan
Summary: [patch] dojox.validate.br isValidCpf error[patch][cla] dojox.validate.br isValidCpf error

We have a patch and CLA, it's to fix a defect for Brazil-specific validation. I'd suggest we consider for 1.10.

comment:7 Changed 5 years ago by dylans <dylan@…>

Resolution: fixed
Status: assignedclosed

In 621e979d4eec2dacb7f5dee370de282477fb46fd/dojox:

Error: Processor CommitTicketReference failed
Unsupported version control system "git": Can't find an appropriate component, maybe the corresponding plugin was not enabled? 

comment:8 Changed 5 years ago by dylans <dylan@…>

In a6fc7db58b7ff72eaa52059f32683fe0914655c4/dojox:

Error: Processor CommitTicketReference failed
Unsupported version control system "git": Can't find an appropriate component, maybe the corresponding plugin was not enabled? 

comment:9 Changed 5 years ago by dylans <dylan@…>

In 8be11f4ba79553b8a74d562966fe10b8ef2ebd7f/dojox:

Error: Processor CommitTicketReference failed
Unsupported version control system "git": Can't find an appropriate component, maybe the corresponding plugin was not enabled? 

comment:10 Changed 5 years ago by dylans <dylan@…>

In 9fa37a19eeec6b9ef7c33b1b3a166498d858b418/dojox:

Error: Processor CommitTicketReference failed
Unsupported version control system "git": Can't find an appropriate component, maybe the corresponding plugin was not enabled? 

comment:11 Changed 5 years ago by dylan

Milestone: 1.101.7.6

backported to 1.9.4, 1.8.7, and 1.7.6

Note: See TracTickets for help on using tickets.