Created
August 1, 2018 15:39
-
-
Save pipech/8498f6760d8fe1c4c41caa2716866f9b to your computer and use it in GitHub Desktop.
ตรวจสอบ เลข บัตรประจำตัวประชาชน python
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
def check_tax_id(id): | |
if len(id) != 13: | |
return False | |
# ขั้นตอนที่ 1 - เอาเลข 12 หลักมา แยกเป็น list | |
# หลักที่ 13 ใช้ตรวจสอบผลลัพธ์ | |
list_id = list(id) | |
sum = 0 | |
for index in range(0, 12): | |
# ขั้นตอนที่ 1.5 - เช็คว่าแต่ละตำแหน่งเป็นตัวเลขหรือไม่ | |
try: | |
int_id = int(list_id[index]) | |
except ValueError: | |
return False | |
# ขั้นตอนที่ 2 - เอาเลข 12 หลักนั้นมา คูณเข้ากับเลขประจำหลักของมัน | |
index_value = int_id * (13 - index) | |
# ขั้นตอนที่ 3 - เอาผลคูณทั้ง 12 ตัวมา บวกกันทั้งหมด | |
sum += index_value | |
# ขั้นตอนที่ 4 - เอาเลขที่ได้จากขั้นตอนที่ 3 มา mod 11 (หารเอาเศษ) | |
remainder = sum % 11 | |
# ขั้นตอนที่ 5 - เอา 11 ตั้ง ลบออกด้วย เลขที่ได้จากขั้นตอนที่ 4 | |
# ถ้าเกิด ลบแล้วได้ออกมาเป็นเลข 2 หลัก ให้เอาเลขในหลักหน่วย มาใช้ | |
digit13 = (11 - remainder) % 10 | |
# ตรวจสอบ ค่าที่ได้ กับ เลขตัวสุดท้ายของ บัตรประจำตัวประชาชน | |
if digit13 == int(list_id[12]): | |
return True | |
else: | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ref.
https://pastebin.com/5MPhhNQH
https://www.baanmaha.com/community/threads/20521-วิธีตรวจสอบรหัสบัตรประชาชน
http://www.thaicreate.com/community/thai-id-card.html
https://gist.github.com/wannaphongcom/b7b8078c5d7f3b60a4af4f4ef2efbfda#file-checkpid-py
http://tools.necz.net/idcard.php