Loading...

如何使用javascript判斷身分證號跟居留證格式

Temperature: 0 °C

ChungChung
author_tools

首先,是把如何用javascript判斷字串中必須包含中英文的方式延伸~

將每一個字轉換成 ASCII 碼。

在使用for迴圈去檢查每個字是不是在  65-90(a-z) 的範圍。

var text='A12345678';
var res = text.toUpperCase(); //將字母由小寫變大寫

var check=[];
check['alpha']=0;
for(var i=0;i[2;i++){
var t=res.charCodeAt(i);

if(t]=65 && t[=90){
check['alpha']++;
}}

因為身分證的第一個字是英文字母,而居留證則是前二個字是英文字母。

因此底下可以先做最基本的判斷,得到的ID是屬於身分證還是居留證~
if(check['alpha']==2){
if (!check_resident_ID(res)){
alert(res+"此居留證字號錯誤、少碼或空格,請確認後重新輸入。")
exit();
}

}else if(check['alpha']==1){
if (!checkID(res)){
alert(res+"此身分證字號錯誤、少碼或空格,請確認後重新輸入。")
exit();
}

}else{
alert('ID錯誤');
}

接著身分證部分的檢查function如下~
function checkID( id ) {
//alert(id)
tab = "ABCDEFGHJKLMNPQRSTUVXYWZIO"
A1 = new Array (1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3 );
A2 = new Array (0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5 );
Mx = new Array (9,8,7,6,5,4,3,2,1,1);

//驗證填入身分證字號長度及格式
if ( id.length != 10 ) return false;
i = tab.indexOf( id.charAt(0) );
if ( i == -1 ) return false;
sum = A1[i] + A2[i]*9;

for ( i=1; i[10; i++ ) {
v = parseInt( id.charAt(i) );
if ( isNaN(v) ) return false;
sum = sum + v * Mx[i];
}
if ( sum % 10 != 0 ) return false;
return true;
}

居留證的檢查function如下~
function check_resident_ID( id ) {

//驗證填入身分證字號長度及格式
if(id.length != 10){
return false;
}

//格式,用正則表示式比對第一個字母是否為英文字母
if(isNaN(id.substr(2,8)) ||
(!/^[A-Z]$/.test(id.substr(0,1))) || (!/^[A-Z]$/.test(id.substr(1,1)))){
return false;
}

var idHeader = "ABCDEFGHJKLMNPQRSTUVXYWZIO"; //按照轉換後權數的大小進行排序
//這邊把身分證字號轉換成準備要對應的
id = (idHeader.indexOf(id.substring(0,1))+10) +
'' + ((idHeader.indexOf(id.substr(1,1))+10) % 10) + '' + id.substr(2,8);
//開始進行身分證數字的相乘與累加,依照順序乘上1987654321

snumber = parseInt(id.substr(0,1)) +
parseInt(id.substr(1,1)) * 9 +
parseInt(id.substr(2,1)) * 8 +
parseInt(id.substr(3,1)) * 7 +
parseInt(id.substr(4,1)) * 6 +
parseInt(id.substr(5,1)) * 5 +
parseInt(id.substr(6,1)) * 4 +
parseInt(id.substr(7,1)) * 3 +
parseInt(id.substr(8,1)) * 2 +
parseInt(id.substr(9,1));

checkNum = parseInt(id.substr(10,1));
//模數 - 總和/模數(10)之餘數若等於第九碼的檢查碼,則驗證成功

console.log(10 - snumber % 10);
if((10 - snumber % 10) == checkNum){
return true;
}
else{
return false;
}

}

以上紀錄~

台北市內湖區江南街71巷75弄 Go

#javascript  #判斷身分證號  #判斷居留證  
https://innstory.com/story-如何使用javascript判斷身分證號跟居留證格式-1267
寫程式筆記

Prev
 如何用javascript判斷字串中必須包含中英文

Next
小老虎探險 

Nearby Attractions

台北市內湖區江南街71巷75弄-Nearby Attractions

  • 臺北工業學校紅樓 Go
  • 紫藤廬 Go
  • 臺灣總督府電話交換局 Go
  • 七海寓所(蔣經國故居) Go
  • 北投文物館 Go
  • 龍安坡黃宅濂讓居 Go

About the Author

Chung

我是chung
網路工作者
主業是網站系統開發建置
副業是做夢,寫故事
作品請參考/teme.biz
做夢請參考/innstory.com
聯絡/chung.teme@gmail.com

#有人用筆寫日記,有人用歲月寫日記,有人用照片寫日記,而我,用innstory寫日記。

Visitor message

Leave some footprints to prove that you visited me

Recommended reading

Author's other related stories

瀏覽器升級ie9後FCKEditor無法使用對話框解決方法

瀏覽器升級ie9後FCKEd...

原文網址:http://ecshop.tw/bbs/archiver/tid.html 升級到 IE...

jQuery效果滑動隱藏

jQuery效果滑動隱藏

因為需要所以紀錄下來,主要是要透過點選按鈕將字串隱藏或顯示出來... 雖說可以直接用hide或是sh...

DIV區塊內文字左右對齊?

DIV區塊內文字左右對齊?

最近常接到一些英文網站建置的專案....為了讓內容文字能夠左右對齊... 所以我用了以下方式 div...

Recommended reading

Other stories happened at this address

嗯,聽說是在模仿小丸子的髮型...

嗯,聽說是在模仿小丸子的髮型...

嗯,聽說是在模仿小丸子的髮型...

有時候覺得毛巾是一個很好用的工具,但前提是她的頭必須一直維持同樣的姿勢才行~

有時候覺得毛巾是一個很好用的...

有時候覺得毛巾是一個很好用的工具,但前提是她的頭必須一直維持同樣的姿勢才行~

過了三點一刻,但還是先來杯南洋咖啡吧~

過了三點一刻,但還是先來杯南...

過了三點一刻,但還是先來杯南洋咖啡吧~

窗

window 有時候,只需要一扇窗,一杯咖啡。 一些安靜的時間......就這樣的沉澱著~暫時拋開一...

2014年的最後一天...

2014年的最後一天...

今天是年月日....年的最後一天... 回顧這一年來...甜頭苦頭都嚐過了.....就連最後一天的今...

人生就應該像這頑強生命力的地瓜葉

人生就應該像這頑強生命力的地...

前幾天,隨手將準備丟棄的地瓜葉梗插進原本要種植香菜的盆裡。 過了快一個星期,竟然多了幾片綠意~ 我的...

Please select an option

error

Hi, thank you for your participation, but you cannot vote repeatedly~

Join innstory now and start recording your story.

"Innstory" is a place to store stories. We are committed to becoming a warm platform. Deepening the bonds between people is our direction.
We are convinced that the blockchain between people is not just a cold calculation. Join us now.

Wrong format