<script src="http://apps.bdimg.com/libs/jquery/1.8.3/jquery.min.js"></script>
输入身份证号码:
<input type="text" name="sfz" id="sfz" maxlength="18" />
<p class="birthday"></p>
<script>
$(document).on("keyup blur","#sfz",function(){
var code=$(this).val();
var len=code.length;
//方法一
/*if ($.trim($(this).val().length)>6)
$(".birthday").html(code.substr(6,4)+'-'+code.substr(10,2)+'-'+code.substr(12,2));
else
$(".birthday").html("");*/
//方法二(推荐)
if (len>6)
$(".birthday").html(code.substr(6,4)+(len>10?'-':'')+code.substr(10,2)+(len>12?'-':'')+code.substr(12,2));
else
$(".birthday").html("");
});
</script>
js实现根据身份证号自动生成出生日期
<!doctype>
<html>
<head>
<script type="text/javascript">
function dealCard(){
var birthday=getBirthday();
switch(birthday){
case 0:alert("Sorry,the program runs wrong!");break;
case 1:alert("You must be input again!");break;
default: alert("the birthday is"+birthday);break;
}
}
function getBirthday(){
var a=document.getElementById("card").value;
if(15==a.length || 18==a.length){
var left=a.length-12;
var right=a.length-4;
var b=a.slice(left,right);
if(8==b.length){
return b;
}
else return 0;
}
else return 1;
}
</script>
</head>
<body>
input <input type="text" id="card" height=20px width=40px/>
<br/>
<button type="button" onclick="dealCard()">please click it</button>
</body>
</html>
<!doctype>
<html>
<head>
<script type="text/javascript">
function getBirthday(){
var a=document.getElementById("card").value;
var left=a.length-12;
var right=a.length-4;
var b=a.slice(left,right);
alert("the birthday is"+b);
}
</script>
</head>
<body>
input <input type="text" id="card" height=20px width=40px/>
<br/>
<button type="button" onclick="getBirthday()">please click it</button>
</body>
</html>