javascript身分証と電話番号の検証
5376 ワード
// 15 18 , 。
// , , 6 。
function isIdCardNo(num)
{
num = num.toUpperCase();
// 15 18 ,15 ,18 17 , , X。
if (!(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(num)))
{
alert(' , !
15 ,18 X。');
return false;
}
// ISO 7064:1983.MOD 11-2 ,X 10。
//
var len, re;
len = num.length;
if (len == 15)
{
re = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/);
var arrSplit = num.match(re);
//
var dtmBirth = new Date('19' + arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4]);
var bGoodDay;
bGoodDay = (dtmBirth.getYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(arrSplit[3])) && (dtmBirth.getDate() == Number(arrSplit[4]));
if (!bGoodDay)
{
alert(' !');
return false;
}
else
{
// 15 18
// ISO 7064:1983.MOD 11-2 ,X 10。
var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
var nTemp = 0, i;
num = num.substr(0, 6) + '19' + num.substr(6, num.length - 6);
for(i = 0; i < 17; i ++)
{
nTemp += num.substr(i, 1) * arrInt[i];
}
num += arrCh[nTemp % 11];
return num;
}
}
if (len == 18)
{
re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/);
var arrSplit = num.match(re);
//
var dtmBirth = new Date(arrSplit[2] + "/" + arrSplit[3] + "/" + arrSplit[4]);
var bGoodDay;
bGoodDay = (dtmBirth.getFullYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(arrSplit[3])) && (dtmBirth.getDate() == Number(arrSplit[4]));
if (!bGoodDay)
{
alert(dtmBirth.getYear());
alert(arrSplit[2]);
alert(' !');
return false;
}
else
{
// 18 。
// ISO 7064:1983.MOD 11-2 ,X 10。
var valnum;
var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
var nTemp = 0, i;
for(i = 0; i < 17; i ++)
{
nTemp += num.substr(i, 1) * arrInt[i];
}
valnum = arrCh[nTemp % 11];
if (valnum != num.substr(17, 1))
{
alert('18 ! :' + valnum);
return false;
}
return num;
}
}
return false;
}
2、厳しい検証(18桁のみ):<script>
var aCity={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 cidInfo(sId){
var iSum=0
var info=""
if(!/^d{17}(d|x)$/i.test(sId))return false;
sId=sId.replace(/x$/i,"a");
if(aCity[parseInt(sId.substr(0,2))]==null)return "Error: ";
sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));
var d=new Date(sBirthday.replace(/-/g,"/"))
if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))return "Error: ";
for(var i = 17;i>=0;i --) iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11)
if(iSum%11!=1)return "Error: ";
return aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?" ":" ")
}document.write(cidInfo("380524198002300016"),"");
document.write(cidInfo("340524198002300019"),"")
document.write(cidInfo("340524197711111111"),"")
document.write(cidInfo("34052419800101001x"),"");
</script>
JavaScriptフォームは電話番号を検証し、入力量が電話番号かどうかを判断し、正規表現で実現します.//電話番号のチェック
function isTel(str){
var reg=/^([0-9]|[\-])+$/g ;
if(str.length<7 || str.length>18){
return false;
}
else{
return reg.exec(str);
}
}