JavaScript : ตรวจสอบหมายเลขบัตรประจำตัวประชาชน
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | function checkID(id) { //ตรวจว่าป้อนถูกตามรูปแบบที่กำหนดมั้ย x-xxxx-xxxxx-xx-x var regExpObj = /^\d{1}\-\d{1,4}\-\d{1,5}\-\d{1,2}\-\d{1}$/; if (regExpObj.test(id) == false) return false; //ตัด - เอาแต่เลขมาตรวจ id = id.replace(/-/g,""); //ตรวจว่ามี 13 หลักถูกมั้ย if (id.length!=13) return false; //เลขนำหน้าของมีได้แค่ 1-8 if( id.charAt(0) < 1 || id.charAt(0) > 8 ) return false; //คำนวณหลักสุดท้าย for(i=0,sum=0;i<12;i++) sum += parseInt(id.charAt(i))*(13-i); sum = sum%11; if(sum <= 1) sum = 1-sum; else sum = 11-sum; return (sum == parseInt(id.charAt(12))); } |
ข้อมูลจาก http://th.wikipedia.org/wiki/เลขประจำตัวประชาชนไทย
Written by Komkid on August 5th, 2009 with
1 comment.
Read more articles on JavaScript and Programming.
- [+] Digg: Feature this article
- [+] Del.icio.us: Bookmark this article
- [+] Furl: Bookmark this article