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

querySelectorAll對多個相同ID做統一處理

querySelectorA...

a 範例,想將同一組ID的DIV做統一隱藏的動作 var elms = document.query...

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

如何用javascript判...

字串拆開逐字檢查方式~ 首先將每一個字轉換成 ASCII 碼。 在使用for迴圈去檢查每個字是不是在...

解決FB分享帶參數問題

解決FB分享帶參數問題

又到了FB分享連結的時間了,沒辦法FB太紅...@@... 一般來說如果是以FB的分享網址做分享的方...

Recommended reading

Other stories happened at this address

啊~是在拍我嗎?

啊~是在拍我嗎?

啊~是在拍我嗎?

屬於自己的片刻

屬於自己的片刻

s 外頭下著雨。 Mandy跟U都已入睡~我卻捨不得入眠。 自從U的到來,照顧著這傢伙,說真的幾乎已...

兒子真幸福,我小時候都沒玩過這麼多火材盒車,感謝恆永跟另一位朋友(雖然我們已經不再連絡)

兒子真幸福,我小時候都沒玩過...

兒子真幸福,我小時候都沒玩過這麼多火材盒車,感謝恆永跟另一位朋友(雖然我們已經不再連絡)

jQuery效果滑動隱藏

jQuery效果滑動隱藏

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

外頭下了一整天的雨,系統裡的天氣卻顯示我所在的位置陽光普照?何解?

外頭下了一整天的雨,系統裡的...

外頭下了一整天的雨,系統裡的天氣卻顯示我所在的位置陽光普照?何解?

清除網頁暫存(no cache)

清除網頁暫存(no cach...

pexelsphoto 在製作專案時,有沒有試過JS 或 CSS 明明就已經更新了~ 但頁面卻像鬼打...

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