jsp社會安全號碼碼驗證代碼(15,18位)

來源:互聯網
上載者:User

<%@ page contenttype="text/html; charset=gb2312" language="java" import="java.sql.*" errorpage="" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>網頁特效p/jsp.html target=_blank >jsp教程社會安全號碼碼驗證代碼(15,18位)</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <label for="textfield"></label>
  <input type="text" name="card" id="textfield" />
  <input type="submit" name="button" id="button" value="提交" />
</form>
</body>
</html>
<%
function checkidcard(idcard){
var errors=new array(
"驗證通過!",
"社會安全號碼碼位元不對!",
"社會安全號碼碼出生日期超出範圍或含有非法字元!",
"社會安全號碼碼校正錯誤!",
"身份證地區非法!"
);
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:"國外"}

var idcard,y,jym;
var s,m;
var idcard_array = new array();
idcard_array = idcard.split("");

//地區檢驗
if(area[parseint(idcard.substr(0,2))]==null) return errors[4];
alert(errors[4]);

//身份號碼位元及格式檢驗
switch(idcard.length){
case 15:
if ( (parseint(idcard.substr(6,2))+1900) % 4 == 0 || ((parseint(idcard.substr(6,2))+1900) % 100 == 0 && (parseint(idcard.substr(6,2))+1900) % 4 == 0 )){
ereg=/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/;//測試出生日期的合法性
} else {
ereg=/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/;//測試出生日期的合法性
}

if(ereg.test(idcard)) return errors[0];
alert(errors[0]);
else return errors[2];
alert(errors[2]);
break;
case 18:
//18位身份號碼檢測
//出生日期的合法性檢查
//閏年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))
//平年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))
if ( parseint(idcard.substr(6,4)) % 4 == 0 || (parseint(idcard.substr(6,4)) % 100 == 0 && parseint(idcard.substr(6,4))%4 == 0 )){
ereg=/^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9xx]$/;//閏年出生日期的合法性Regex
} else {
ereg=/^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9xx]$/;//平年出生日期的合法性Regex
}
if(ereg.test(idcard)){//測試出生日期的合法性
//計算校正位
s = (parseint(idcard_array[0]) + parseint(idcard_array[10])) * 7
+ (parseint(idcard_array[1]) + parseint(idcard_array[11])) * 9
+ (parseint(idcard_array[2]) + parseint(idcard_array[12])) * 10
+ (parseint(idcard_array[3]) + parseint(idcard_array[13])) * 5
+ (parseint(idcard_array[4]) + parseint(idcard_array[14])) * 8
+ (parseint(idcard_array[5]) + parseint(idcard_array[15])) * 4
+ (parseint(idcard_array[6]) + parseint(idcard_array[16])) * 2
+ parseint(idcard_array[7]) * 1
+ parseint(idcard_array[8]) * 6
+ parseint(idcard_array[9]) * 3 ;
y = s % 11;
m = "f";
jym = "10x98765432";
m = jym.substr(y,1);//判斷校正位
if(m == idcard_array[17]) return errors[0]; //檢測id的校正位
else return errors[3];
alert(errors[3]);
}
else return errors[2];
alert(errors[2]);
break;
default:
return errors[1];
alert(errors[1]);
break;
}
}

%>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.