格式是否正确输入年、月、日判断

用prompt的形式:
<!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> </body> </html> <script> var N =prompt('请输入年') if(N!=null){ var b =isNaN(N) if(b==false) { if(N>0&&N<10000&&N%1==0) { var Y =prompt('请输入月') if(Y!=null) { var b1=isNaN(Y) if(b1==false) { if(Y>0&&Y<13&&Y%1==0) { var R=prompt('请输入日') if(R!=null) { var f =isNaN(R) if(f==false) { if(R>0&&R<32&&R%1==0) { if(Y==1||Y==3||Y==5||Y==7||Y==8||Y==10||Y==12) { alert(N+''+Y+''+R+'') } } if(R>0&&R<31&&R%1==0) { if(Y==4||Y==6||Y==9||Y==11) { alert(N+''+Y+''+R+'') } } else(alert('输入有误')) } if(Y==2) {if(N%4==0&&N%100!==0||N%400==0) { alert(N+''+Y+''+R+'') } else if(R>0&&R<29&&R%1==0) { alert(N+''+Y+''+R+'') } else(alert('输入有误')) } } } else(alert('输入有误')) } else(alert('输入有误')) } } else(alert('输入有误')) } else(alert('输入有误')) } </script>


用函数的形式:

<!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="number" id="n" placeholder="年" />
<input type="number" id="y" placeholder="月"/>
<input type="number" id="r" placeholder="日" />
<input type='button' value="点击" onclick="xx()" />
</body>
</html>
<script>
function xx()
{
var n=document.getElementById('n').value;
var y=document.getElementById('y').value;
var r=document.getElementById('r').value;
if(n!=''&&y!=''&&r!='')
{
var qq=parseFloat(n)
var pp=parseFloat(y)
var oo=parseFloat(r)
if(n%1==0&&y%1==0&&r%1==0)
{
if(n>0&&n<10000)
{
if(y>0&&y<13)
{
if(y==1||y==3||y==5||y==7||y==8||y==10||y==12)
{
if(r>0&&r<32)
{
alert(n+'年'+y+'月'+r+'日')
}
else
{
alert('你输入的日有错误')
}
}
else if(y==4||y==6||y==9||y==11)
{
if(r>0&&r<31)
{
alert(n+'年'+y+'月'+r+'日')
}
else
{
alert('你输入的日有错误')
}
}
else
{
if(n%4==0&&n%100!=0||n&400==0)
{
if(r>0&&r<30)
{
alert(n+'年'+y+'月'+r+'日')
}
else
{
alert('你输入的日有错误')
}
}
else
{
if(r>0&&r<29)
{
alert(n+'年'+y+'月'+r+'日')
}
else
{
alert('你输入的日有错误')
}
}
}
}
else
{
alert('你输入的月份有错误')
}
}
else
{
alert('你输入的年分有错误')
}
}
else
{
alert('输入错误')
}
}
}
</script>

原文地址:https://www.cnblogs.com/dandan1224/p/5801496.html