I have this validate functions:
function unformat (value) {
if (!value) {
return '';
}
return value.replace(/^0+|[^0-9kK]+/g, '').toUpperCase();
}
function validate(value) {
const unformatted = unformat(value);
if (/^0+/.test(unformatted)) {
return false;
}
let remainer = parseInt(unformatted.slice(0, -1), 10);
let module = 1;
let counter = 0;
while (remainer > 0) {
module = (module + (remainer % 10) * (9 - counter++ % 6)) % 11;
remainer = Math.floor(remainer / 10);
}
const verifier = module > 0 ? '' + (module - 1) : 'K';
return verifier === unformatted.slice(-1);
}
console.log(validate('14.211.109-8-')); // true, it should be false
console.log(validate('14.211.109-8 ')); // true, it should be false
https://jsfiddle.net/pmiranda/0nb51myp/5/
The idea is ot get false in two case when right now is getting true:
- When the string ends in `` // -
- When the string ends in
// blankspace
How could I modify that validate function?