Javascript:数据验证表单

时间:2015-05-10 19:00:24

标签: javascript

我有一个我在Javascript中制作的数据验证程序,它工作正常,它基本上显示特定日期的额外信息,例如,它在哪一天,以及其他信息。

我的问题是我不知道如何加入HTML标记。

这是我的任务截图: enter image description here

var canvas;
canvas = openGraphics();
var day;
day=prompt( "Please enter your day of birth");
var month;
month = prompt( "Please enter your month of birth");
var year;
year = prompt( "Please enter your year of birth");
var date;
date = new Date( year, month-1,day);
if(true){
if(date.getFullYear()== year )
{   
}
if( date.getMonth()== month-1 )
{               
}
if( date.getDate()== day )
{
}
else{
alert( "Invalid Date" );
}
}
canvas.setFont( "Palatino Linotype", "24px", Font.PLAIN );
canvas.setColor("blue"); 
canvas.drawString( "Full DOB:", 10, 10 );
canvas.drawString( date, 100, 10 );
canvas.paint();

到目前为止我尝试过HTML,这是完全错误的,我需要有人向我展示如何将上面的代码实现为html格式:

<html>
<head>
<title>
Date Validation
</title>
<script>
function checkdate(){

var year = document.getElementById('year').value;
var month = document.getElementById('month').value;
var day = document.getElementById('day').value;

var date = new Date( year, month-1,day);

if(true)

{

if(date.getFullYear()== year )
if( date.getMonth()== month-1 )               
if( date.getDate()== day )

}

else{
alert( "Invalid Date" );
}
</script>
<body>
<h1>Data Validation</h1>
<p> This page will be used to provide information on the specific date that  you    enter below. </p>
<form>
Day:
<input type = "text" input id="day" onchange = "checkdate();>
</form>
<form>
Month:
<input type = "text" input id="month" onchange = "checkdate();>
</form>
<form>
Year:
<input type = "text" input id="year" onchange = "checkdate();">
</form>
<form>
<input type="submit" value="Validate Date">
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您需要关闭引号,并从行中间删除input。而不是

<form>
Day:
<input type = "text" input id="day" onchange = "checkdate();>
</form>
<form>
Month:
<input type = "text" input id="month" onchange = "checkdate();>
</form>
<form>
Year:
<input type = "text" input id="year" onchange = "checkdate();">
</form>

使用

<form>
Day:
<input type="text" id="day" onchange="checkdate()">
</form>
<form>
Month:
<input type="text" id="month" onchange="checkdate()">
</form>
<form>
Year:
<input type="text" id="year" onchange="checkdate()">
</form>

另外,避免使用onchange="checkdate();"

等分号是个好习惯