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

PHP取得檔案路徑及檔案名稱

PHP取得檔案路徑及檔案名稱...

例如我有一支檔案是放在根目錄下的demo資料夾中,檔案名稱為test.php 、首先取得目前網頁的檔...

PHP使用GD函數切正方形圖像

PHP使用GD函數切正方形圖...

基本上大都是等比例將圖片上傳.... 但有時也會有需要正方形邊長的圖像需求 這時候還是得靠GD函式了...

分享 (励志短片) 王德顺 《最炫东北人》 完整版 Wang Deshun - Coolest Northeasterner (Inspirational Short Film)

分享 (励志短片) 王德顺 ...

我們都還年輕啊~ 還是可以一直做夢下去的。 只要有夢,我將可以走的更遠。 我是chung 主業做夢,...

Recommended reading

Other stories happened at this address

小黃瓜拌炒豬肉片佐高粱

小黃瓜拌炒豬肉片佐高粱

s 最近氣候多變,有時候冷得不像話~ 因此,原本想要拿來涼拌的小黃瓜,也改變了製作方式~用炒的( ̄∇...

PDO連接Mysql方式

PDO連接Mysql方式

pexelsphoto 最近因為源碼檢測的原因,才驚覺自己以前的系統撰寫的方式早已不再安全。 所以決...

你是否知道我故事中的角色?

你是否知道我故事中的角色?

那天~突然在想....... 有看我故事的人,是否會知道我寫的那些故事裡,裏頭的人事物對我有何意義?...

童言童語,也太有禮貌篇...

童言童語,也太有禮貌篇......

U最近喜歡說的單字是「謝謝 」,而且也學會了在單字後面加上對象。 遞個東西給他,會說謝謝(爸爸),會...

剪髮初體驗~

剪髮初體驗~

第天,剪髮初體驗~由阿婆親自操刀。 這傢伙很淡定啊,看來他根本不知道即將要發生什麼事。 果然,阿婆還...

排列組合年紀~

排列組合年紀~

s 不知不覺的到了開始喜歡上排列組合遊戲的年紀。 總是把玩具或車車拿到某個角落空間。 然後開始堆疊起...

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