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
  • 福州街11號日式宿舍 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

定義404頁面

定義404頁面

什麼是頁面? HTTP 或Not Found錯誤訊息是HTTP的其中一種「標準回應訊息」(HTTP狀...

使用dp.SyntaxHighlighter 程式碼色彩顯示工具,讓紀錄程式心得更有可看性

使用dp.SyntaxHig...

說好了要在innstory裡寫下故事紀錄... 所以是不是有方便紀錄的工具或方式,就變成我在開發in...

javascript 底下使用 htmlspecialchars

javascript 底下使...

pexelsphoto Javascript中使用一個函數達到PHP的htmlspecialchar...

Recommended reading

Other stories happened at this address

所以你現在是有多老?

所以你現在是有多老?

這天,U指著自己小時候的照片開心的說這是我年輕的時候。 所以你現在是有多老?

好吧~原來貼紙是這樣玩的...看來還不夠他貼啊~

好吧~原來貼紙是這樣玩的.....

好吧~原來貼紙是這樣玩的...看來還不夠他貼啊~

5U日常

5U日常

img滿歲的U,喜歡把自己硬擠進小盒子裡。 他以為他是貓? img然後,他找到根香蕉。

傳聲筒,還好小時候有上過自然科學。

傳聲筒,還好小時候有上過自然...

用吃完的優格杯子自製了傳聲筒。 我跟U玩的不亦樂乎。 當然我小學的記憶是養樂多瓶子加上牙籤,但反正原...

爸爸,卡住啊~

爸爸,卡住啊~

前陣子U學會個單字。 一個是「怕怕 」,另一個是「 卡住啊」 而且這個單字似乎是他目前最喜歡使用的。...

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

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

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

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