Last active
September 13, 2021 12:05
-
-
Save lavary/6ba2b6d1d561f517eb997c77b699777c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const validateNif = function (value) { | |
return { | |
valid: validateNIF(value), | |
errorMessage: 'NIF não válido' | |
} | |
} | |
function validateNIF (value) { | |
if (!/[123]\d{8}/.test(value)) { | |
return false | |
} | |
let cd = parseInt(value[8]) | |
let sum = 0 | |
for (let i = 9, idx = 0; i >= 2; i--, idx++) { | |
sum += parseInt(value[idx]) * i | |
} | |
var rem = sum % 11 | |
if (rem < 2) { | |
return cd === 0 | |
} else { | |
return cd === 11 - rem | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment