您的位置:首页 > Web前端 > JavaScript

JS练手:18位身份证号码验证

2007-09-18 11:34 411 查看
简易18位身份证号码验证
可以验证身份证长度,格式,日期,校验码的正确性,对于地区码要想验证比较复杂,没有对其处理,有需要的朋友可以到中华人民共和国国家统计局网站上看下各个时期的行政区划代码(http://www.stats.gov.cn/tjbz/xzqhdm/index.htm





<
html
>



<
head
>



<
META
http-equiv
="pragma"
content
="no-cache"
>



<
META
http-equiv
="Cache-Control"
content
="no-cache"
>



<
META
http-equiv
="Expires"
content
="0"
>



<
META
http-equiv
='content-type'
content
="text/html; charset=gb2312"
>



<
META
http-equiv
="Content-Style-Type"
content
="text/css"
>





<
style
>
...



div





{...}
{



width
:
100%
;



text-align
:
center
;



color
:
blue
;



font-size
:
12pt
;



background-color
:;



}



tbody





{...}
{



font-size
:
10pt
;



}



tr





{...}
{



height
:
30px
;



}



td





{...}
{



text-align
:
left
;



vertical-align
:
middle
;



}



.textbox





{...}
{



text-align
:
center
;



}



.button





{...}
{



width
:
70px
;



cursor
:
hand
;



}





.fl:first-letter
{...}
{



color
:
red
;



font-size
:
15pt
;



font-weight
:
bolder
;



font-style
:
italic
;



text-decoration
:;



}



</
style
>





<
script
type
='text/javascript'
>
...





var
vertifyStr
=

"
10X98765432
"
;



var
wiArray
=

new
Array(
7
,
9
,
10
,
5
,
8
,
4
,
2
,
1
,
6
,
3
,
7
,
9
,
10
,
5
,
8
,
4
,
2
,
1
);





function
anlyseIDCard()





...
{



var
idcard
=
document.getElementById(
'
cidcard
'
).value;





var
reg
=

new
RegExp(
/^
d
...
{
17
}

(d
|
x)$
/
gi);



if
(
!
reg.test(idcard))





...
{



document.getElementById(
'
error
'
).style.display
=
""
;



return
;





}

else
...
{



document.getElementById(
'
error
'
).style.display
=
"
none
"
;



}





var
result
=

0
;



result
+=
checkArea(idcard);



result
+=
checkBirthday(idcard);



result
+=
checkSort(idcard);



result
+=
checkVertify(idcard);



if
(result
==

0
)





...
{



document.getElementById(
'
checkresult
'
).innerText
=
unescape(
'
%u6709%u6548%u7684%u8EAB%u4EFD%u8BC1%u53F7%u7801%uFF01
'
);



}



else





...
{



document.getElementById(
'
checkresult
'
).innerText
=
unescape(
'
%u65E0%u6548%u7684%u8EAB%u4EFD%u8BC1%u53F7%u7801%uFF01
'
);



}





}





function
checkArea(idcard)





...
{



return
showResult(
true
,
'
darea
'
,idcard.substring(
0
,
6
));



}





function
checkBirthday(idcard)





...
{



var
bir
=
idcard.substring(
6
,
14
);



return
showResult(isDate(bir),
'
dbirthday
'
,bir);



}





function
checkSort(idcard)





...
{



return
showResult(
true
,
'
dsort
'
,idcard.substring(
14
,
17
));



}





function
checkVertify(idcard)





...
{



var
id
=
idcard.substring(
0
,
17
);



var
vty1
=
idcard.substring(
17
,
18
);



var
vty2
=
getVertifyChar(id)



return
showResult(vty1.toUpperCase()
==
vty2,
'
dvertify
'
,vty1);



}





function
getVertifyChar(str)





...
{



var
total
=

0
;



for
(
var
i
=

0
; i
<=
str.length; i
++
)





...
{



total
+=
eval(str.charAt(i)
*
wiArray[i]);



}



total
=
total
%
11
;



return
vertifyStr.charAt(total);



}







function
showResult(flg,id,val)
...
{





with
(document.getElementById(id))
...
{



innerText
=
val;





if
(
!
flg)
...
{



style.color
=
'
red
'
;



return

1
;





}

else
...
{



style.color
=
'
blue
'
;



return

0
;





}



}



}





function
cwrite(ustr)





...
{



document.write(unescape(ustr));



}





function
appChars(num,
char
,length)





...
{



var
str
=
num.toString(
10
);



while
(str.length
<
length)





...
{



str
=
char
+
str;



}



return
str;



}





function
isDate(str)





...
{



if
(str.length
!=

8
)
return

false
;



var
date
=

new
Date(str.substring(
0
,
4
),eval(str.substring(
4
,
6
)
-
1
),str.substring(
6
,
8
));



var
str2
=
appChars(date.getFullYear(),
"
0
"
,
4
)
+
appChars(eval(date.getMonth()
+
1
),
"
0
"
,
2
)
+
appChars(date.getDate(),
"
0
"
,
2
);



if
(str
==
str2)



return

true
;



else



return

false
;



}





function
isNumber(str)





...
{





var
re
=

new
RegExp(
/^
d
...
{
1
,}

$
/
g);



return
re.test(str);



}



</
script
>



</
head
>



<
body
>



<
hr
color
='blue'
/>



<
table
border
='1'
cellspacing
= '0'
cellpadding
='0'
width
='780px'
align
='center'
>



<
caption
style
="color:blue"
>
Check IDCard
</
caption
>



<
tbody
>



<
tr
>





<
td
style
="width:20%;text-align:right;"
><
script
>
...
cwrite(
'
%u8981%u9A8C%u8BC1%u7684%u53F7%u7801%uFF1A
'
);

</
script
></
td
>



<
td
style
="width:80%;"
>



<
input
class
='textbox'
type
='text'
style
='width:200px'
id
='cidcard'
/>



<
input
class
='button'
type
='button'
value
='Check'
onclick
='anlyseIDCard();'
/>





<
font
id
='error'
style
="display:none;color:red;font-size:9pt;"
><
script
>
...
cwrite(
'
%u53F7%u7801%u683C%u5F0F%u4E0D%u6B63%u786E%uFF01
'
);

</
script
></
font
>



</
td
>



</
tr
>



<
tr
>



<
td
colspan
='2'
style
="width:100%;"
>



<
table
border
='0'
cellspacing
= '1'
cellpadding
='2'
width
='100%'
align
='center'
>



<
tbody
>



<
tr
>





<
th
><
script
>
...
cwrite(
'
%u5730%u5740%u7801
'
);

</
script
></
th
>





<
th
><
script
>
...
cwrite(
'
%u51FA%u751F%u65E5%u671F
'
);

</
script
></
th
>





<
th
><
script
>
...
cwrite(
'
%u987A%u5E8F%u7801
'
);

</
script
></
th
>





<
th
><
script
>
...
cwrite(
'
%u6821%u9A8C%u7801
'
);

</
script
></
th
>



</
tr
>



<
tr
style
='background-color:#eeeeee'
>



<
td
><
div
id
='darea'
></
div
></
td
>



<
td
><
div
id
='dbirthday'
></
div
></
td
>



<
td
><
div
id
='dsort'
></
div
></
td
>



<
td
><
div
id
='dvertify'
></
div
></
td
>



</
tr
>



</
tbody
>



</
table
>



<
td
>



</
tr
>



<
tr
>



<
td
style
='color:blue;text-align:right;'
>





<
script
>
...
cwrite(
'
%u9A8C%u8BC1%u7ED3%u679C%uFF1A
'
);

</
script
>



</
td
>



<
td
>



<
div
id
='checkresult'
class
='fl'
style
='font-size:10pt;text-align:left;'
></
div
>



</
td
>



</
tr
>



</
tbody
>



</
table
>



<
hr
color
='blue'
>



</
body
>



</
html
>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: