I can't seem to get the variable for year working,I tried this and it should work. I am new to JS and HTML please forgive the naive questions
HTML page that includes a simple form. ask the visitor to type in his/her name, and year he/she was born. Write a JavaScript to extract the data from the form when the submit button is clicked. The script should display the following information in a new document. Hello, Visitor's name, you are xx years old. (Your script should show an error message if a visitor type in a future year.)
<!DOCTYPE html>
<html>
<body>
<form action="demo_form.asp">
First name: <input type="text" id="FirstName" ><br>
Birth year: <input type="integer" id="BirthYear" ><br>
<input type="button" value="Submit" onclick = "test1();">
</form>
<script>
var curYear = today.getFullYear();
function test1(){
var yy = document.getElementById("FirstName").value;
var xx = document.getElementById("BirthYear").value;
if( xx < curYear){
xx = curYear - xx;
document.write("Hello," + yy + ", you are " + xx + " years old");
}
else{
alert("Your age is invalid");
}
}
</script>
</body>
</html>
You haven't instantiated today
:
var today = new Date(); // add this line
var curYear = today.getFullYear(); // now `today` will resolve
You need to instantiate the variable today
. Add var today = new Date();
before the var curYear = today.getFullYear();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.