0

I would like to create a function to validate if a DOB date i.e: 10/05/2002 is allowed to register this year 2020, for example if user selects the three dropdowns 10 05 2020(today's date) it should be allowed to register and pass this validation because today turns 18.

Currently code is only validating using year value from Date object and selected year from dropdown.

function myFunction(Year) {//2020

            var Year = 2020 > selected dropdown
            var month = 10  > selected dropdown
            var day = 05    > selected dropdown
//Today's day selected since today user turns 18 years old

            var birthdate = month + '/' + day + '/' + Year;

            var age = new Date().getFullYear() - new Date(birthdate).getFullYear();
       //Day and Month Validation here

            if (age <= 18) {
                    alert('Not Allowed To Register');
                }
                else {
                    alert('Allowed to register');
                }
        }

If dropdown option selections = From January 1st untill today(Oct 05 2020) it should be allowed to register Validation should be against Day and Month also not just the year to be 18 years old.

This is what I've tried after doing some research and different answers, but boolean value(dob_this_year) in calculateDOB() function still false and not validating correctly.

function MyFunction(year) {

            var Year = 2020;
            var month = 10;
            var day = 05;

            var birthdate = month + '/' + day + '/' + Year;
            var isAllowedToRegister = calculateDOB(birthdate);


            //var age = new Date().getFullYear() - new Date(birthdate).getFullYear();
            if (!isAllowedToRegister) {
                    alert('Allowed to Register');
                }
                else {
                    alert('Not Allow to register yet!');
                }
        }

        function calculateDOB(date) {
            var now = new Date();
            var current_year = now.getFullYear();
            var year_diff = current_year - new Date(date).getFullYear();
            var birthday_this_year = new Date(current_year, new Date(date).getMonth, new Date(date).getDate());
            var dob_this_year = (now >= birthday_this_year);

            return dob_this_year ? true : false;
        }
RTFL
  • 145
  • 1
  • 7
Alexander
  • 1
  • 1
  • Did you have a look on this question? ... https://stackoverflow.com/questions/55851164/how-to-validate-a-users-birth-date-in-javascript-with-a-regular-expression – Ahmed Saeed Oct 05 '20 at 18:52
  • Firstly, you are passing birthdate to the function and it is a string not a date - you need to create a new date from that. Secondly, as dob_this_year is already a boolean, you only need to return that if you want true/false passed back. – ATD Oct 05 '20 at 18:56
  • Thanks, will take a look. – Alexander Oct 05 '20 at 19:40
  • There are already a [huge number of questions](https://stackoverflow.com/search?q=%5Bjavascript%5D+difference+between+dates) on getting the difference between two dates, please do some research. Also, creating a string to parse is a poor way to create a date, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Oct 05 '20 at 21:34

0 Answers0