身分証番号の検証-javascriptスクリプト
6078 ワード
javascriptを使って身分証番号の検証:
//
function checkidCard (idCard) {
//
if(idCard == '')
{
alert(' , ');
$("#personQueryCondition.zjhm").focus;
return false;
}
// ,
if(isIdCardNo(idCard) == false)
{
alert(' , ');
$("#personQueryCondition.zjhm").focus;
return false;
}
//
if(checkProvince(idCard) == false)
{
alert(' , ');
$("#personQueryCondition.zjhm").focus;
return false;
}
//
if(checkBirthday(idCard) == false)
{
alert(' , ');
$("#personQueryCondition.zjhm").focus();
return false;
}
return true;
}
//
function checkOfficerCard (officerCard) {
//
if(officerCard == '')
{
alert(' , ');
$("#personQueryCondition.zjhm").focus;
return false;
}
return true;
}
//
function checkPass(pass) {
//
if(pass == '')
{
alert(' , ');
$("#personQueryCondition.zjhm").focus;
return false;
}
return true;
}
function checkQueryCondition() {
var zjlx = $("#personQueryConditionZjlx").val();
var zjhm = $("#personQueryConditionZjhm").val();
var cxmd = $("#personQueryConditionCxmd").val();
var xm = $("#personQueryConditionXm").val();
var cxsy = $("#personQueryConditionCxsy").val();
if(zjlx == null || zjlx == "") {alert(" "); return false;}
if(zjhm == null || zjhm == "") {alert(" "); return false;}
if(cxmd == null || cxmd == "") {alert(" "); return false;}
if(cxsy == null || cxsy == "") {alert(" ");return false;}
if(xm != null && xm != "") {
var reg=/[ - ]/g;
if (reg.test(xm)){
var txt = new RegExp("[\\(,\\),\\!,\\`,\\#,\\$,\\%,\\^,\\&,\\@,\\',\\&,\\\\,\\/,\\|,\\:,\\<,\\>,\"]");
if(txt.test(xm)) {
alert(" , !");
return false;
}
var regDig = /\d/g;
if(regDig.test(xm)){
alert(" , !");
return false;
}
} else {
alert(" , !");
return false;
}
}
return true;
}
var vcity = { 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:" "
};
// , ,
function isIdCardNo(idCard) {
// 15 18 ,15 ,18 17 , , X
var reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/;
if(reg.test(idCard) == false)
{
return false;
}
return true;
}
// ,
function checkProvince(idCard) {
var province = idCard.substr(0,2);
if(vcity[province] == undefined)
{
return false;
}
return true;
}
//
function checkBirthday(idCard) {
var len = idCard.length;
// 15 , (3 ) (3 ) (2 ) (2 ) (2 ) (3 ),
if(len == '15')
{
var re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/;
var arr_data = idCard.match(re_fifteen);
var year = arr_data[2];
var month = arr_data[3];
var day = arr_data[4];
var birthday = new Date('19'+year+'/'+month+'/'+day);
return verifyBirthday('19'+year,month,day,birthday);
}
// 18 , (3 ) (3 ) (4 ) (2 ) (2 ) (4 ), X
if(len == '18')
{
var re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/;
var arr_data = idCard.match(re_eighteen);
var year = arr_data[2];
var month = arr_data[3];
var day = arr_data[4];
var birthday = new Date(year+'/'+month+'/'+day);
return verifyBirthday(year,month,day,birthday);
}
return false;
}
//
function verifyBirthday(year,month,day,birthday) {
var now = new Date();
var now_year = now.getFullYear();
//
if(birthday.getFullYear() == year && (birthday.getMonth() + 1) == month && birthday.getDate() == day)
{
// (3 100 )
var time = now_year - year;
if(time >= 3 && time <= 100)
{
return true;
}
return false;
}
return false;