Skip to content

Instantly share code, notes, and snippets.

@GaussianWonder
Created June 21, 2022 13:57
Show Gist options
  • Save GaussianWonder/702589673c33297f8167d3118291f5d1 to your computer and use it in GitHub Desktop.
Save GaussianWonder/702589673c33297f8167d3118291f5d1 to your computer and use it in GitHub Desktop.
Romanian CNP
/**
* 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