试着自己写一下,就会了
<!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=utf-8" />
<title>无标题文档</title>
</head>
<body>
身份证:<input type="text" id="sfz" onblur="fun()"/>
生日:<input type="text" id="sr" />
性别:
<select id="sex">
<option>男</option>
<option>女</option>
</select>
<script type="text/javascript">
function fun()
{
var sfz = document.getElementById("sfz");
var sr  = document.getElementById("sr");
var sex = document.getElementById("sex");
if(sfz.value.length!=18)
{
alert("生日错误");
return;
}

sr.value = sfz.value.substring(6,14).replace(/(\d{4})(\d{2})(\d{2})/,"$1年$2月$3日");
var s = sfz.value.substring(14,17).replace(/^0*/,"");
if( parseInt(s,10)%2==0 )
sex.selectedIndex = 1;
else
sex.selectedIndex = 0;
}
</script>
</body>
</html>

解决方案 »

  1.   

    function showBirthday(val)
      {
       var birthdayValue;
       if(15==val.length)
       { //15位身份证号码
        birthdayValue = val.charAt(6)+val.charAt(7);
        if(parseInt(birthdayValue)<10)
        {
         birthdayValue = '20'+birthdayValue;
        }
        else
        {
         birthdayValue = '19'+birthdayValue;
        }
        birthdayValue=birthdayValue+'-'+val.charAt(8)+val.charAt(9)+'-'+val.charAt(10)+val.charAt(11);
        if(parseInt(val.charAt(14)/2)*2!=val.charAt(14))
         document.all.sex.value='男';
        else
         document.all.sex.value='女';
        document.all.birthday.value=birthdayValue;
       }
       if(18==val.length)
       { //18位身份证号码
        birthdayValue=val.charAt(6)+val.charAt(7)+val.charAt(8)+val.charAt(9)+'-'+val.charAt(10)+val.charAt(11)     +'-'+val.charAt(12)+val.charAt(13);
        if(parseInt(val.charAt(16)/2)*2!=val.charAt(16))
         document.all.sex.value='男';
        else
         document.all.sex.value='女';
        if(val.charAt(17)!=IDCard(val))
        {
         document.all.idCard.style.backgroundColor='#ffc8c8';
        }
        else
        {
         document.all.idCard.style.backgroundColor='white';
        }
        document.all.birthday.value=birthdayValue;
       }
      }  // 18位身份证号最后一位校验
      function IDCard(Num)
      {
       if (Num.length!=18)
        return false;
       var x=0;
       var y='';   for(i=18;i>=2;i--)
        x = x + (square(2,(i-1))%11)*parseInt(Num.charAt(19-i-1));
       x%=11;
       y=12-x;
       if (x==0)
        y='1';
       if (x==1)
        y='0';
       if (x==2)
        y='X';
       return y;
      }  // 求得x的y次方
      function square(x,y)
      {
       var i=1;
       for (j=1;j<=y;j++)
        i*=x;
       return i;
      }
      </script>
      

  2.   

    参考
    http://topic.csdn.net/u/20081111/11/4d1e42a8-8749-40ec-a215-bea9229b7374.html?56860247
      

  3.   

    非常感谢varlj 大哥,身份证有15位和18位的,你能不能分开给我判断下啊我追加50分给你
      

  4.   


    <!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=utf-8" />
        <title>无标题文档</title>
    </head>
    <body>
    身份证:<input type="text" id="sfz" onblur="fun()"/>
    生日:<input type="text" id="sr" />
    性别:
    <select id="sex">
        <option>男</option>
        <option>女</option>
    </select>
    <script type="text/javascript">
    function fun()
    {
        var sfz = document.getElementById("sfz");
        var sr  = document.getElementById("sr");
        var sex = document.getElementById("sex");
        if(sfz.value.length!=18 && sfz.value.length!=15)
        {
            alert("生日错误");
            return;
        }
        var birthday;
        var sexflag;
        if(sfz.value.length==18)
        {
         birthday = sfz.value.substring(6,14);
         sexflag = sfz.value.substring(14,17).replace(/^0*/,"");
        }
        else
        {
         birthday = "19"+sfz.value.substring(6,12);
         sexflag = sfz.value.charAt(14);
        }
        
        sr.value = birthday.replace(/(\d{4})(\d{2})(\d{2})/,"$1年$2月$3日");
        var s = sexflag.replace(/^0*/,"");
        if( parseInt(s,10)%2==0 )
            sex.selectedIndex = 1;
        else
            sex.selectedIndex = 0;
    }
    </script>
    </body>
    </html>