-1

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>
0

2 Answers 2

1

You need to instantiate the variable today. Add var today = new Date(); before the var curYear = today.getFullYear();

Sign up to request clarification or add additional context in comments.

Comments

1

You haven't instantiated today:

var today = new Date();              // add this line
var curYear = today.getFullYear();   // now `today` will resolve

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.