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.

Related articles

1 comment

Read the comments left by other users below, or:

Trackback Mention from Komkid.net
#1. December 10th, 2009, at 11:15 PM.

JavaScript : AJAX for Province, Amphur, Tumbol | KomKid.Net: ๆ ดียังไง ให้เปิด ตรวจสอบหมายเลขบัตรประจำตัวประชาชน ประกอบครับ (ที่จริงต้องเปิด

Leave your comment...

If you want to leave your comment on this article, simply fill out the next form:

You have to be identified to write a comment.