adsense

2012-05-09

[javascript] 身分證檢查








<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[34]='I'
local[18]='J'
local[19]='K'
local[20]='L'
local[21]='M'
local[22]='N'
local[35]='O'
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('輸入的字號長度有誤!')
document.CodeForm.ID.value="填入您的身分證字號"
}else if(firstlettererr(ID)){
alert('身分證第一碼查無此英文字母:'+ID.substring(0,1))
document.CodeForm.ID.value="填入您的身分證字號"
}else if(numerr(ID)){
alert('輸入的身分證後九碼應為數字!')
document.CodeForm.ID.value="填入您的身分證字號"
}else if(checkerr(ID)){
alert('您輸入的身分證檢查碼有誤!')
document.CodeForm.ID.value="填入您的身分證字號"
}
}

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<=35;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<=35;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<=9;i++){
if(i==0)
we=we+se[i]
else
we=we+(se[i]*(10-i))
}

if(mod(we,10)!=0)
checkcode=((10-mod(we,10)))
else
checkcode=0
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>
</head>

<body>
<form name="f" onsubmit="checkid(document.f.ID.value)">
<input name="ID" id="ID" value='填入您的身分證字號'
onFocus="if (this.value=='填入您的身分證字號') this.value='';"
onblur="checkID(document.CodeForm.ID.value);document.CodeForm.ID.value=document.CodeForm.ID.value.toUpperCase()"
size="27" maxlength="10" style="float: left">

<input type="button" name="bname" value="確認身分證" onclick="checkID(document.f.ID.value)">
</form>


沒有留言:

張貼留言