首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

输入身份证号,自动显示生日,该如何处理

2012-02-07 
输入身份证号,自动显示生日inputname news_identcard type text class myinput id news_ident

输入身份证号,自动显示生日
<input   name= "news_identcard "   type= "text "   class= "myinput "   id= "news_identcard "   value= " "   size= "18 "   maxlength= "18 ">

鼠标离开这个input后

出生年月框,自动显示年月

[解决办法]
...........
所有的大陆居民身份证 的出身日期都是从第6位开始的.......
先来了解一下身份证的标准
----------------------------------------------
身份证原有的15位号码在升18位时基本不变,只是在出生年前加两位(例如王先生是1975年出生的,他的身份证号码是132826750111161, 其中132826为区位码,75代表1975年出生,升为18位后将改为1975,即在75前加19,将出生年补充完整);另外在身份证号码末位后再补充一位数字,这位数字是随机的,因而不再具有其他意义,仅用于识别.(原来的身份证号码末位单数代表男性,偶数代表女性.上面讲到的王先生升位前身份证号码末位是1,是单数,代表男性,而升位后身份证号码变为132826197501111618,末位是8变为了偶数.)部分人的身份证升为18位后末位还会出现X这个字母,而不是数字.这都是很正常的.
----------------------------------------------

<script type= "text/javascript ">
var str1= "132826750111161 ";
var str2= "522512198512122134 ";
document.write(str1.substring(6,6)) ; <!--15位-->
document.write(str2.substring(6,8)) ; <!--18位-->
</script>


一个字符串函数就搞定了啊!!!
SubString()


[解决办法]
上面的写错了。。。。。。。。。。。

代码如下:

<!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> 无标题文档 </title>
<script type= "text/javascript ">
function test()
{
var aaa;
aaa = document.getElementById( "num ");
if(aaa.value.length == 15)
{
alert(aaa.value.substring(6,12));
}
else if(aaa.value.length == 18)
{
alert(aaa.value.substring(6,14));
}

}
</script>


</head>
<body>
<input id= "num " name= "num " value= " " onblur= "test() "/>
</body>
</html>

stringObject.substring(start,stop)

start
开始位置 Required. Where to start the extraction. Must be a numeric value
必选项。开始选取的位置。必须为数字。
stop
停止 Optional. Where to stop the extraction. Must be a numeric value
可选项。停止选取的位置。必须为数字。

[解决办法]
lenber() 提供的信息完全正确...

<pre>
所有的大陆居民身份证 的出身日期都是从第6位开始的.......
先来了解一下身份证的标准
----------------------------------------------
身份证原有的15位号码在升18位时基本不变,只是在出生年前加两位(例如王先生是1975年出生的,
他的身份证号码是132826750111161, 其中132826为区位码,75代表1975年出生,升为18位后将改为1975,
即在75前加19,将出生年补充完整);另外在身份证号码末位后再补充一位数字,这位数字是随机的,
因而不再具有其他意义,仅用于识别.(原来的身份证号码末位单数代表男性,偶数代表女性.上面讲到的王先生升位前身份证号码末位是1,是单数,代表男性,
而升位后身份证号码变为132826197501111618,
末位是8变为了偶数.)部分人的身份证升为18位后末位还会出现X这个字母,而不是数字.这都是很正常的.
</pre>

<script type= "text/javascript ">
// <![CDATA[
var IdFt = "445121820929367 ";
var IdSt = "445121198209293677 ";
var IdSt1 = "445121198209293677x ";

document.write(fGetBirthDayById(IdFt, false, true), " <hr/> ");
document.write(fGetBirthDayById(IdFt, true, true).toLocaleDateString(), " <hr/> ");

document.write(fGetBirthDayById(IdSt, false, true), " <hr/> ");
document.write(fGetBirthDayById(IdSt1, true, true).toLocaleDateString(), " <hr/> ");



function fGetBirthDayById(Id, bReturnDateObj, bDebug)
{// shawl.qiu code : return DateObj Or String
var Debug = bDebug;
if(Id.constructor == String||Id.constructor == Number)
{
Id = [Id];
}
Id[0]+= " ";
Id[0] = Id[0].replace(/^(\d{18}|\d{15})(?:[\s\S]*)/, "$1 ");

var D, M, Y;

switch(Id[0].length)
{
case 15:
D = 19+Id[0].slice(6, 8);
M = Id[0].slice(8, 10);
Y = Id[0].slice(10, 12);
break;

case 18:
D = Id[0].slice(6, 10);
M = Id[0].slice(10, 12);
Y = Id[0].slice(12, 14);
break;

default:
throw new Error( "请输入正确的身份证号码! ");
break;
}

if(bDebug)
{
document.write( "Id[0]: ", Id[0], " <br/> ");
document.write( "Id[0].length: ", Id[0].length, " <br/> ");
document.write( "D: ", D, " <br/> ");
document.write( "M: ", M, " <br/> ");
document.write( "Y: ", Y, " <br/> ");
document.write( " <p/> ");
}

if(bReturnDateObj)
{
return new Date(Y, M, D);
}

return [Y, "- ", M, "- ", D].join( " ");
} // end function fGetBirthDayById(Id, bReturnDateObj, bDebug)
//]]>
</script>
[解决办法]
晕...犯了两个错误...


<pre>
所有的大陆居民身份证 的出身日期都是从第6位开始的.......
先来了解一下身份证的标准
----------------------------------------------
身份证原有的15位号码在升18位时基本不变,只是在出生年前加两位(例如王先生是1975年出生的,
他的身份证号码是132826750111161, 其中132826为区位码,75代表1975年出生,升为18位后将改为1975,
即在75前加19,将出生年补充完整);另外在身份证号码末位后再补充一位数字,这位数字是随机的,
因而不再具有其他意义,仅用于识别.(原来的身份证号码末位单数代表男性,偶数代表女性.上面讲到的王先生升位前身份证号码末位是1,是单数,代表男性,
而升位后身份证号码变为132826197501111618,
末位是8变为了偶数.)部分人的身份证升为18位后末位还会出现X这个字母,而不是数字.这都是很正常的.
</pre>

<script type= "text/javascript ">
// <![CDATA[
var IdFt = "445121820929367 ";
var IdSt = "445121198209293677 ";
var IdSt1 = "445121198209293677x ";

document.write(fGetBirthDayById(IdFt, false, true), " <hr/> ");
document.write(fGetBirthDayById(IdFt, true, true).toLocaleDateString(), " <hr/> ");

document.write(fGetBirthDayById(IdSt, false, true), " <hr/> ");
document.write(fGetBirthDayById(IdSt1, true, true).toLocaleDateString(), " <hr/> ");

function fGetBirthDayById(Id, bReturnDateObj, bDebug)
{// shawl.qiu code : return DateObj Or String
var Debug = bDebug;
if(Id.constructor == String||Id.constructor == Number)
{
Id = [Id];
}
Id[0]+= " ";
Id[0] = Id[0].replace(/^(\d{18}|\d{15})(?:[\s\S]*)/, "$1 ");

var D, M, Y;

switch(Id[0].length)
{
case 15:
Y = 19+Id[0].slice(6, 8);
M = (Id[0].slice(8, 10)-0).toString(10);
D = Id[0].slice(10, 12);
break;

case 18:
Y = Id[0].slice(6, 10);
M = Id[0].slice(10, 12);
D = Id[0].slice(12, 14);
break;

default:
throw new Error( "请输入正确的身份证号码! ");
break;
}

if(bDebug)
{
document.write( "Id[0]: ", Id[0], " <br/> ");
document.write( "Id[0].length: ", Id[0].length, " <br/> ");


document.write( "Y: ", Y, " <br/> ");
document.write( "M: ", M, " <br/> ");
document.write( "D: ", D, " <br/> ");
document.write( " <p/> ");
}

if(bReturnDateObj)
{
M-=1;
return new Date(Y, M, D);
}

return [Y, "- ", M, "- ", D].join( " ");
} // end function fGetBirthDayById(Id, bReturnDateObj, bDebug)
//]]>
</script>

热点排行