首頁 >web前端 >js教程 >js身分證判斷方法支援15位元和18位元_javascript技巧

js身分證判斷方法支援15位元和18位元_javascript技巧

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB原創
2016-05-16 16:55:021049瀏覽
複製程式碼 程式碼如下:

//HTML頁面上要有一個id為identity_card的input輸入框,一個id為ipmessage的身份證錯誤或正確時提示訊息的地方
<script> <BR>//身份證驗證<BR>$(document).ready(function(){ <BR>$("#identity_card").change(function(){ <BR>var idcard =$(this).val(); <BR> checkDate(idcard ); <BR>}); <BR>}); <BR>//身分證<BR>function checkDate( idcard ){ <BR>var socialNo = idcard; <BR>if(socialNo == " ") <BR>{ <BR>$('#ipmessage').html('輸入身分證號碼格式不正確,必須是15位到18位的身分證號'); <BR>alert("輸入身份證號不能為空!"); <BR>return (false); <BR>} <BR>if (socialNo.length != 15 && socialNo.length != 18) <BR>{ <BR>$(' #ipmessage').html('輸入身分證號碼格式不正確,必須是15位到18位的身分證號碼'); <BR>alert("輸入身分證號碼格式不正確!"); <BR> return (false); <BR>} <BR>var area={11:"北京",12:"天津",13:"河北",14:"山西",15:"內蒙古",21:"遼寧",22:"吉林",23:"黑龍江",31:"上海",32:"江蘇",33:"浙江",34:"安徽",35:"福建",36:"江西", 37:"山東",41:"河南",42:"湖北",43:"湖南",44:"廣東",45:"廣西",46:"海南",50:"重慶",51: "四川",52:"貴州",53:"雲南",54:"西藏",61:"陝西",62:"甘肅",63:"青海",64:"寧夏",65:"新疆",71:"台灣",81:"香港",82:"澳門",91:"國外"}; <BR>if(area[parseInt(socialNo.substr(0,2))]==null) { <BR>$('#ipmessage').html('輸入身分證號碼格式不正確,必須是15位到18位的身分證號'); <BR>alert("身分證號碼不正確(地區非法)!"); <BR>return (false); <BR>} <BR>if (socialNo.length == 15) <BR>{ <BR>pattern= /^d{15}$/; <BR>if (pattern.exec(socialNo)==null){ <BR>$('#ipmessage').html('輸入身分證號碼格式不正確,必須是15位到18位的身分證號'); <BR>alert("15位身分證號碼必須為數字! "); <BR>return (false); <BR>} <BR>var birth = parseInt("19" socialNo.substr(6,2)); <BR>var month = socialNo.substr(8,2) ; <BR>var day = parseInt(socialNo.substr(10,2)); <BR>switch(month) { <BR>case '01': <BR>case '03': <BR>case '05' : <BR>case '07': <BR>case '08': <BR>case '10': <BR>case '12': <BR>if(day>31) { <BR>$('# ipmessage').html('輸入身分證號碼格式不正確,必須是15位到18位的身分證號'); <BR>alert('輸入身分證號碼不格式正確!'); <BR>return false; <BR>} <BR>break; <BR>case '04': <BR>case '06': <BR>case '09': <BR>case '11': <BR>if(day> 30) { <BR>$('#ipmessage').html('輸入身分證號碼格式不正確,必須是15位到18位的身分證號'); <BR>alert('輸入身分證號碼不格式正確!'); <BR>return false; <BR>} <BR>break; <BR>case '02': <BR>if((birth % 4 == 0 && birth % 100 != 0) | | birth % 400 == 0) { <BR>if(day>29) { <BR>$('#ipmessage').html('輸入身分證號碼格式不正確,必須是15位到18位的身份證號'); <BR>alert('輸入身分證號碼不正確!'); <BR>return false; <BR>} <BR>} else { <BR>if(day>28) { <BR>$('#ipmessage').html('輸入身分證號碼格式不正確,必須是15位到18位的身分證號碼'); <BR>alert('輸入身分證號碼不正確!』) ; <BR>return false; <BR>} <BR>} <BR>break; <BR>default: <BR>$('#ipmessage').html('輸入身分證號碼格式不正確,必須是15位到18位的身分證號'); <BR>alert('輸入身分證號碼不正確!'); <BR>return false; <BR>} <BR>var nowYear = new Date().getYear (); <BR>if(nowYear - parseInt(birth)<15 || nowYear - parseInt(birth)>100) { <BR>$('#ipmessage').html('輸入身分證號碼格式不正確,必須是15位到18位的身分證號'); <BR>alert('輸入身分證號碼不格式正確!'); <BR>return false; <BR>} <BR>$('#ipmessage' ).html('通過! '); <BR>return (true); <BR>} <BR>var Wi = new Array( <BR>7,9,10,5,8,4,2,1,6, <BR>3, 7,9,10,5,8,4,2,1 <BR>); <BR>var lSum = 0; <BR>var nNum = 0; <BR>var nCheckSum = 0; <BR>for (i = 0; i < 17; i) <BR>{ <BR>if ( socialNo.charAt(i) < '0' || socialNo.charAt(i) > '9' ) <BR>{ <BR>$( '#ipmessage').html('輸入身分證號碼格式不正確,必須是15位到18位的身分證號碼'); <BR>alert("輸入身分證號碼格式不正確!"); <BR>return (false); <BR>} <BR>else <BR>{ <BR>nNum = socialNo.charAt(i) - '0'; <BR>} <BR>lSum = nNum * Wi[i]; <BR>} <BR>if( socialNo.charAt(17) == 'X' || socialNo.charAt(17) == 'x') <BR>{ <BR>lSum = 10*Wi[17]; <BR>} <BR>else if ( socialNo.charAt(17) < '0' || socialNo.charAt(17) > '9' ) <BR>{ <BR>$('#ipmessage').html( '輸入身分證號碼格式不正確,必須是15位到18位的身分證號碼'); <BR>alert("輸入身分證號碼格式不正確!"); <BR>return (false); <BR>} <BR>else <BR>{ <BR>lSum = ( socialNo.charAt(17) - '0' ) * Wi[17]; <BR>} <BR>if ( (lSum % 11) == 1 ) <BR>{ <BR>$('#ipmessage').html('通過!'); <BR>return true; <BR>} <BR>else <BR>{ <BR>$('#ipmessage').html('輸入身分證號碼格式不正確,必須是15位到18位的身分證號碼'); <BR>alert("輸入身分證號碼格式不正確!"); <BR>return (false); <BR>} <BR>} <BR></script>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn