偵測身分證

將下列程式碼複製至<head>程式碼</head>區內

<script language="JavaScript">
<!--
var local=new Array(34)
local[10]='A'
local[11]='B'
local[12]='C'
local[13]='D'
local[14]='E'
local[15]='F'
local[16]='G'
local[17]='H'
local[18]='J'
local[19]='K'
local[20]='L'
local[21]='M'
local[22]='N'
local[23]='P'
local[24]='Q'
local[25]='R'
local[26]='S'
local[27]='T'
local[28]='U'
local[29]='V'
local[32]='W'
local[30]='X'
local[31]='Y'
local[33]='Z'

function checkid(id){
id=id.toUpperCase()
if(lengtherr(id)){
alert('輸入的字號長度有誤!')
}else if(firstlettererr(id)){
alert('身分證第一碼查無此英文字母:'+id.substring(0,1))
}else if(numerr(id)){
alert('輸入的身分證後九碼應為數字!')
}else if(checkerr(id)){
alert('您輸入的身分證檢查碼有誤!')
}else{
alert('您輸入的身分證字號完全正確!')
}
}
function lengtherr(id){
if(id.length<10)
return 1
else
return 0
}
function firstlettererr(id){
var fl=id.substring(0,1)
var haserr=1
for(i=10;i<=33;i++){
if(local[i]!=fl)
continue
else{
haserr=0
break
}
}
if(haserr==1)
return 1
else
return 0
}function numerr(id){
var haserr=0
for(i=1;i<=9;i++){
if(parseInt(id.substring(i,i+1))>0 || id.substring(i,i+1)=='0')
continue
else{
haserr=1
break}
}
if(haserr==1)
return 1
else
return 0
}
function checkerr(id){
var se=new Array(10)
var we=0
var checkcode=0
for(i=10;i<=33;i++){
if(local[i]==id.substring(0,1)){
se[0]=parseInt((i+'0').substring(0,1))
se[1]=parseInt((i+'0').substring(1,2))
break
}
}
for(i=1;i<=9;i++){
se[i+1]=parseInt(id.substring(i,i+1))
}
for(i=0;i<=10;i++){
if(i==0)
we=we+se[i]
else
we=we+(se[i]*(10-i))
}
checkcode=((10-mod(we,10))+'0').substring(0,1)
if(checkcode!=id.substring(9,10))
return 1
else
return 0

}
function mod(a,b){
var r
r=Math.round(a/b)
if((b*r)>a)
r-=1
return (a-(b*r))
}
// -->
</script>

<form name="f" onsubmit="checkid(document.f.id.value)">
<input type="text" size="20" maxlength="10" name="id" value='填入您的身分證字號' onFocus="if (this.value=='填入您的身分證字號') this.value='';">
<input type="button" name="bname" value="確定" onclick="checkid(document.f.id.value)">
</form>