เช็คเลขบัตร ปชช.
This commit is contained in:
parent
68394fe07c
commit
a529207692
9 changed files with 60 additions and 12 deletions
|
|
@ -1,3 +1,5 @@
|
|||
import HttpStatus from "../interfaces/http-status";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
class Extension {
|
||||
public static ToThaiMonth(value: number) {
|
||||
switch (value) {
|
||||
|
|
@ -119,6 +121,40 @@ class Extension {
|
|||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
public static CheckCitizen(value: string) {
|
||||
let citizen = value
|
||||
if (citizen.length !== 13) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "กรุณากรอกข้อมูลรหัสบัตรประจำตัวประชาชนให้ครบ 13 หลัก",);
|
||||
}
|
||||
const citizenIdDigits = citizen.toString().split("").map(Number);
|
||||
const cal =
|
||||
citizenIdDigits[0] * 13 +
|
||||
citizenIdDigits[1] * 12 +
|
||||
citizenIdDigits[2] * 11 +
|
||||
citizenIdDigits[3] * 10 +
|
||||
citizenIdDigits[4] * 9 +
|
||||
citizenIdDigits[5] * 8 +
|
||||
citizenIdDigits[6] * 7 +
|
||||
citizenIdDigits[7] * 6 +
|
||||
citizenIdDigits[8] * 5 +
|
||||
citizenIdDigits[9] * 4 +
|
||||
citizenIdDigits[10] * 3 +
|
||||
citizenIdDigits[11] * 2;
|
||||
const calStp2 = cal % 11;
|
||||
let chkDigit = 11 - calStp2;
|
||||
if (chkDigit === 10) {
|
||||
chkDigit = 1;
|
||||
}
|
||||
else if (chkDigit === 11) {
|
||||
chkDigit = chkDigit % 10;
|
||||
}
|
||||
|
||||
if (citizenIdDigits[12] !== chkDigit) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง");
|
||||
}
|
||||
return citizen;
|
||||
}
|
||||
}
|
||||
|
||||
export default Extension;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue