Created
June 21, 2022 13:57
-
-
Save GaussianWonder/702589673c33297f8167d3118291f5d1 to your computer and use it in GitHub Desktop.
Romanian CNP
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
/** | |
* Validate CNP | |
* @param {string} value CNP | |
* @returns {boolean} Valid or not | |
*/ | |
export const cnp = (value: string) => { | |
var re = /^\d{1}\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])(0[1-9]|[1-4]\d| 5[0-2]|99)\d{4}$/, | |
bigSum = 0, | |
rest = 0, | |
ctrlDigit = 0, | |
control = '279146358279', | |
i = 0; | |
if (re.test(value)) { | |
for (i = 0; i < 12; i++) | |
bigSum += Number(value[i]) * Number(control[i]); | |
ctrlDigit = bigSum % 11; | |
if (ctrlDigit === 10) | |
ctrlDigit = 1; | |
if (ctrlDigit !== parseInt(value[12], 10)) return false; | |
else return true; | |
} | |
return false; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment