ga('set', 'anonymizeIp', 1);
首先 html 代碼如下:
<label for="user_id"><span style="color: red;">*  </span>身分證字號</label> <input id="user_id" type="text" /> <input id="id_submit" type="submit" />
再來,JavaScript代碼:
/*ID verifying */
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"];
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]
$((){
$("#id_submit").click((){
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
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
–以下為驗證方法–