I want to create an age calculator where the user enters a year and the year is subtracted from 2018 and a message displays "Your age in 2018 is..". So if the user enters the year 2000, then the alert message should display Your age in 2018 is 18 years old.However the year inserted should be between 1970 and 2017. This i am unable to do. In addition, i am unable to display the "years old" after the age.How to solve these 2 problems. These are the codes i've tried.
<html>
<head>
<script type="text/javascript">
function age(){
let x = document.getElementById('txtYear').value;
if(x.length == 0) {
alert("Numbers should be between 1970 and 2018");
return;
}
if(isNaN(x)){
alert("Value entered is not numeric");
return;
}
var age = 2018-parseInt(x);
document.getElementById('age').innerText="Your age in 2018 is "+age;
}
</script>
</head>
Enter Year of Birth: <input type="text" id="txtYear" />
<button onClick="age(txtYear.value)">Calculate Age</button>
<br><br>
<div id="age">
</div>
</body>
</html>