Last active
December 15, 2024 11:37
-
-
Save esaramago/de08f44188bbeb89611809680598c9e2 to your computer and use it in GitHub Desktop.
Validação NIF português (typescript)
This file contains hidden or 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
| function validateNIF(value: string) { | |
| // has 9 digits? | |
| if (/^[0-9]{9}$/.test(value) === false) return false | |
| // starts with 5 (Pessoa coletiva) | |
| if(!['1', '2', '3', '5', '6', '8'].includes(value.substring(0,1)) && | |
| !['45', '70', '71', '72', '77', '79', '90', '91', '98', '99'].includes(value.substring(0,2))) | |
| return false | |
| const total = | |
| Number(value[0]) * 9 + | |
| Number(value[1]) * 8 + | |
| Number(value[2]) * 7 + | |
| Number(value[3]) * 6 + | |
| Number(value[4]) * 5 + | |
| Number(value[5]) * 4 + | |
| Number(value[6]) * 3 + | |
| Number(value[7]) * 2 | |
| const module11 = total - Math.floor(total / 11) * 11 | |
| const comparison = module11 === 1 || module11 === 0 ? 0 : 11 - module11 | |
| return Number(value[8]) === comparison | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment