0

I am new to JavaScript with absolutely little idea about the language. I am trying to put an age restriction in a job application form where for the date of birth text field, date format is dd/mm/yyyy and applicants must be at between 15 and 80 years old at the time they fill in the form otherwise they won't be able to apply. I do not want to embed it into HTML file but write it in .js file only.

for DOB input type is text, name is dob, id is dob, pattern is (0[1-9]|1[0-9]|2[0-9]|3[01])/(0[1-9]|1[012])/[0-9]{4}

Thank you.

user8595022
  • 1
  • 1
  • 1
  • 1
    This is something you need to do at the server. – Pointy Sep 12 '17 at 02:13
  • 1
    Possible duplicate of [How do I get the number of days between two dates in JavaScript?](https://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) – stealththeninja Sep 12 '17 at 02:30
  • This seems to be a duplicate of date validation, parsing and difference between two dates. You should cross those bridges one at a time. As Pointy says, do it on the server as you can't trust client side code for this anyway. – RobG Sep 12 '17 at 02:31
  • Doing it client-side is fine for the user's convenience, as long as you do it server-side as well. Anyway, note that your regex doesn't guarantee a valid date, because it accepts, e.g., "31/02/2000". By the way, why is there an upper age limit? Do age discrimination laws allow that where you live? – nnnnnn Sep 12 '17 at 02:35
  • @nnnnnn Its part of my college assignment. I don't have a say. Lol. I have to do it in the client-side server only. – user8595022 Sep 12 '17 at 04:53

2 Answers2

0

You can use min and max attributes of HTML5 input date

HTML:

    <input type="date" id="txtDate" />

JavaScript :

        var dtToday = new Date();
        var month = dtToday.getMonth() + 1;
        var day = dtToday.getDate();
        var year = dtToday.getFullYear();
        var maxYear = year - 18;
        if(month < 10)
            month = '0' + month.toString();
        if(day < 10)
            day = '0' + day.toString();

        var maxDate = maxYear + '-' + month + '-' + day;
        var minYear = year - 80;
        var minDate = minYear + '-' + month + '-' + day;
        alert(maxDate);
        document.querySelectorAll("#txtDate")[0].setAttribute("max",maxDate);

        document.querySelectorAll("#txtDate")[0].setAttribute("min",minDate);
ankesh jain
  • 171
  • 4
  • I have input type = "text" and not date as per the assignment – user8595022 Sep 12 '17 at 04:55
  • It restrict the user from apply with an alert if their age is not between 18-50 years and it should calculate it from the date of birth the user enters. – user8595022 Sep 12 '17 at 05:00
  • Keep type="text" and validate the date using pattern if satisfied then validate the age using @GavinLuo code and display alert when it returns false. – ankesh jain Sep 12 '17 at 05:05
  • As soon as I open the page an alert prompts saying "This Site Says 1999-09-13".. I don't want this prompt to come when I open that page. – user8595022 Sep 12 '17 at 23:15
  • @user8595022 Please remove alert(maxDate) from the code for testing purpose i have included – ankesh jain Sep 13 '17 at 00:56
-1

function processDate(date){
   var parts = date.split("/");
   return new Date(parts[2], parts[1] - 1, parts[0]);
}

function calcAge(date) {
  var dBirth = processDate(date);
  var dToday = new Date();
  var diff = dToday.getTime() - dBirth.getTime();
  return Math.floor(diff / (1000 * 60 * 60 * 24 * 365.25));
}

function validateDate(date){
  var age = calcAge(date);
  console.log(age);
  if(15<=age && age <=80) return true;
  else {
    return false;
  }
}

console.log(validateDate("01/12/1988"));

console.log(validateDate("02/11/1911"));
AlexLuo
  • 458
  • 1
  • 4
  • 12