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

使FCKeditor所上傳的圖檔檔名亂數化

使FCKeditor所上傳的...

編輯"../fckor/or/filemanager/connectors/php/com...

HTML5 Geolocation

HTML5 Geolocat...

說真的,HTML5 Geolocation的功能真的好用... 輕鬆就可以讓網站也能取得目前所在的地...

行事曆功能導入

行事曆功能導入

cc55 Innstory有行事曆囉! 我試著把Innstory當作是我的筆記本,將生活發生的故事紀...

Recommended reading

Other stories happened at this address

5U的副食品時間,採用小雕哥提供的有機南瓜~

5U的副食品時間,採用小雕哥...

U的副食品時間,採用小雕哥提供的有機南瓜~

串接opendata氣象資訊

串接opendata氣象資訊...

研究了幾天的opendata(政府資訊開放平台)https:...

這笑容是想甜死誰啊~

這笑容是想甜死誰啊~

回到台北幾天了~這幾天常見到他笑。 原本有些小擔心....因為在南部住了一段時間,回到台北後不知道他...

看來白川鄉,得再去一次了~

看來白川鄉,得再去一次了~

看來白川鄉,得再去一次了~

很久沒畫了~2015年11月29日這是我第一張嘗試畫在手機裡~

很久沒畫了~2015年11月...

s 小時候~喜歡畫畫,但其實是模仿著家姐的腳步~ 我姐姐以前畫畫很灑脫~隨便幾筆、隨便上色~看起來都...

笑成這樣叫我怎能不愛妳

笑成這樣叫我怎能不愛妳

笑成這樣叫我怎能不愛妳~ G,個多月,還不會翻身但是會抬頭,只是角度還需要多練習。 喜歡坐著多過於躺...

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