cnpj validator javascript
let valida = (cnpj) => {
cnpj = cnpj.replace(/[^\d]+/g, '');
// verificando se tem a quantidade certa de caracter e se não tem todos caracteres iguais
if(cnpj.length !== 14 || /^(\d)\1+$/.test(cnpj))
return false;
let t = cnpj.length - 2,
d = cnpj.substring(t),
d1 = parseInt(d.charAt(0)),
d2 = parseInt(d.charAt(1)),
calc = x => {
let n = cnpj.substring(0, x),
y = x - 7,
s = 0,
r = 0;
for (let i = x; i >= 1; i--) {
s += n.charAt(x - i) * y--;
if (y < 2)
y = 9;
}
r = 11 - s % 11;
return r > 9 ? 0 : r;
}
return calc(t) === d1 && calc(t + 1) === d2;
}
Also in JavaScript:
- fullscreen electron
- JSONDecoder
- node ubutun
- how to get csrf token in javascript
- React Children map example
- How to write on a web page javascript
- create javascript button
- sum array without loop javascript
- javascript random number not decimal
- regex char or char
- format number thousands k javascript
- ... operator javascript
- get request jquery
- spotify api error parsing json
- datatable refresh in js
- device height react native
- how to inject service in component angular 6
- how to make a inventory in js
- _axios2.default.all is not a function
- js if mobile browser
- how to copy text in the clipboard in js
- get the last option from select jquery
- angular on back skip routes
- nodejs fs create file if not exists