Home > Blockchain >  How do I make it where you can only be between ages 15 and 80 when inputting date of birth
How do I make it where you can only be between ages 15 and 80 when inputting date of birth

Time:10-03

window.onload = function() {
  var date = new Date();
  var dd = date.getDate();
  var mm = date.getMonth()   1;
  var yyyy = date.getFullYear();

  //Add a zero if one Digit (eg: 05,09)
  if (dd < 10) {
    dd = "0"   dd;
  }

  //Add a zero if one Digit (eg: 05,09)
  if (mm < 10) {
    mm = "0"   mm;
  }

  minYear = yyyy - 80; //Calculate Minimun Age (<80)
  maxYear = yyyy - 18; //Calculate Maximum Age (>18)

  var min = minYear   "-"   mm   "-"   dd;
  var max = maxYear   "-"   mm   "-"   dd;

  document.getElementById("start").setAttribute("min", min);
  document.getElementById("start").setAttribute("max", max);
};
<form>
  <label for="start">Date of birth:</label>
  <input type="date" id="start" required="required" name="trip-start" value="2021-10-02" />
  <input type="submit" />
</form>

What exactly am I doing wrong here? My JavaScript isn't working. It only works as inline JavaScript but for some reason it nullifies my other JavaScript code and still doesn't work, so I made a separate external JS file to test it but it doesn't work.

Note: It cannot be inline JavaScript.

CodePudding user response:

Also you have to set event listener

  function getEvent(){
var age = getElementById("start").value;
if(age<18 || age> 80){
 console.log("age should be between 18 and 80")
}
}
<input type"submit" onclick="getEvent()" name="click here">
  • Related