台灣身分證字號輸入驗證
HTML, JavaScript, jQuery
首先 html 代碼如下:
1 2 3 |
<label for="user_id"><span style="color: red;">*  </span>身分證字號</label> <input id="user_id" type="text" /> <input id="id_submit" type="submit" /> |
- 新增一個input欄位讓使用者輸入身分證字號
- 新增一input按鈕為功能是submit
再來,JavaScript代碼:
- 首先是 ID verification function.
1 2 3 4 5 6 7 8 9 |
/*ID verifying function*/ function id_verifying(a){ ///////////// id verifying var /////////////// // verify the id format is correct or not var id_string = a; var UpperCase = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]; var LowerCase = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]; // corresponding lacation numbers. var Location_num = ["10","11","12","13","14","15","16","17","34","18","19","20","21","22","35","23","24","25","26","27","28","29","30","31","32","33"]; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
var getFirstChar = id_string.substr(0,1); var getRestNumber = id_string.substr(1,9); var getFirstChar_num = 0; var haveMatch = 0; ///////////// id verifying var /////////////// if(id_string.length==10){ for(var i=0;i<10;i++){ if(id_string[i]==" "){ alert("身分證字號欄位不可包含空白。"); eval("$('#new_patient_id').focus()"); break; }else{} } // verify the first char for(var j=0;j<UpperCase.length;j++){ if(getFirstChar==UpperCase[j]){ getFirstChar_num = Location_num[j]; haveMatch = 1; break; }else if(getFirstChar==LowerCase[j]){ haveMatch = 2; break; }else{haveMatch = 0;} } // if first char is correct // verify the next 9 numbers if(isNaN(getRestNumber)){ alert("請輸入正確的身分證字號格式。"); eval("$('#new_patient_id').focus()"); return 0; }// if first char not valid else if(haveMatch == 0){ alert("請輸入正確的身分證字號格式。"); eval("$('#new_patient_id').focus()"); return 0; }// if first char not uppercase else if(haveMatch == 2){ alert("請將字首英文字元轉為大寫。"); eval("$('#new_patient_id').focus()"); return 0; }else{ // if the input char-count is 10 and including 1 uppercase letter and 9 numbers // than check the static format of the taiwanese id var digitNum = getFirstChar_num.substr(1,1); var decNum = getFirstChar_num.substr(0,1); // digit*9 + decimal var calulate = parseInt(digitNum)*9 + parseInt(decNum); // calulate & verify for(var m=1;m<=8;m++){ calulate += parseInt(id_string[m])*(9-m); } var checkNum = id_string[9]; var totalcheck = (calulate%10 == 0) ? 0 : (10-calulate%10); if(checkNum == totalcheck){ return 1; }else{ alert("請輸入正確的身分證字號格式。"); eval("$('#new_patient_id').focus()"); return 0; } |
}
[/code]
- call function 部分
先做 submit 按鈕被按下要判斷 input 欄位是否有內容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$(function(){ $("#id_submit").click(function(){ var no_blank = 0; // detect if there are blank input areas. if($("#user_id").val()==""){ eval("$(‘#user_id’).focus()"); }else{ no_blank = 1; var id_correct = 0; var id_string = $("#user_id").val(); // call id verify function id_correct = id_verifying(id_string); } // if all correct do submit.(Here we show an alert.) if((no_blank&&id_correct)==1){alert("Correct!");} else{} }); }); |
註: 身分證格式判定規則
我們的身分證是一位英文字母加上九位數字,
下面這組就是理論上合規則的身分證字號,
A 1 2 3 4 5 6 7 8 9
首先,字母部分A對應到10,B是11,其他如下:
A 10|B 11 |C 12|D 13|E 14|F 15|G 16|H 17|I 34|J 18|K 19|L 20|M 21|
N 22|O 35|P 23|Q 24|R 25|S 26|T 27|U 28|V 29|W 32|X 30|Y 31|Z 33
–以下為驗證方法–
- 英文轉成的數字,個位數乘9再加上十位數的數字,
例如若是A,就是0x9+1=1。 - 再來是接下來的9碼,
前八碼由左至右分別乘以由1開始到8結束的升冪數列。
直接看例子:
1 2 3 4 5 6 7 8 | 9
x x x x x x x x
8 7 6 5 4 3 2 1 升冪數列
等於
8+14+18+20+20+18+14+8 = 120。 - 將 1.及 2.所得的數字相加。
1+120=121。 - 再來要看第九碼,第九碼為檢查碼,本例的檢查碼是 9。
用檢查碼加以驗證:
求出 3. 除以10之後的餘數(mod),再用10減去該餘數,得到的結果應該要與檢查碼相同。
(求餘數時要注意,若餘數為零,檢查碼就是 0,不需要再用10做減的動作。)
用例子來看:
121 / 10 = 12 ….. 1
10 – 1 = 9
9 = 9(第九碼檢查碼),所以最後知道這個身分證字號是合法的。
留言