js实现根据身份证号自动生成出生日期

<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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值