date of birth validation if user is above 16 - javascript

Trying to validate if the user is above 16 but it is failing the condition everytime
here is my code
trying to add a condition to check if user is atleast 16 years old
if (($("#year").val(), month, day)) {
Here is the try
if (($("#year").val(), month, day) <= getAge(new Date())) {
where getAge is a function like this
function getAge(DOB) {
var today = new Date();
var birthDate = new Date(DOB);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
but it is still going into condition if i enter date as 10/22/2001

Your getAge function returns 16 as per your '10/22/2001' example.
But your if statement is strange. You don't need to find out someones birthdate if they were born today.
You can use:
if (getAge($("#year").val()) < 16) {
// the person is 15 or under
}
else {
// the person is 16 or over.
}

Your question and the given code is not that clear. I believe IsDate is a function to check if the input is a valid date, If yes pass that value to getAge function.
if(getAge(**enter your DOB**)>=16){
//write your code if the age is greater than or equal to 16
}

Related

How do I get the user's age in years

I would like to use Geolocation and Date in JavaScript (ECMA) to ask the user where and when they were born and convert this into years. I am asking where they were born because I need to take into account different time zones.
So far, my function returns the number of years the user is old:
function birth() {
var their_birth_day = new Date("18 May 2005"); // Just as an example
var today = new Date();
var ms_old = today.getTime() - their_birth_day.getTime();
var yrs_old = ms_old/3.154e+10;
return yrs_old;
}
But how do I use their location to make it more accurate?
function getAge( dateString ) {
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}

How to calculate new date after date validation?

I'm using Elian Ebbing's data validation code from here and after validation, I'd like to take the date entered and return a new date for X amount of months later. For example, if I entered 06/09/2019, I would then like the code to return the correct new date that's 6 months later, which would be 12/6/2019.
Can someone please help guide me through the process of accomplishing this? I have been trying different methods of reusing the original code to get the results that I want, however I have been at this since July 2nd and have concluded I just can't figure this out on my own. I am completely stumped.
Lastly, my deepest apologies in advance that I didn't just comment on the original thread for Mr. Ebbing's code and ask for help, but unfortunately I did not have enough reputation points to do so.
If you are not sure that it is good to use some library (moment.js). If you want to find something already discovered, be ready to bump your head.
// Elian Ebbing validator
function isValidDate(dateString) {
// First check for the pattern
if(!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateString))
return false;
// Parse the date parts to integers
var parts = dateString.split("/");
var day = parseInt(parts[1], 10);
var month = parseInt(parts[0], 10);
var year = parseInt(parts[2], 10);
// Check the ranges of month and year
if(year < 1000 || year > 3000 || month == 0 || month > 12)
return false;
var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
// Adjust for leap years
if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
monthLength[1] = 29;
// Check the range of the day
return day > 0 && day <= monthLength[month - 1];
}
// if you want to change date format
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1), // monts start form 0 so for result 06/01/2019
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) {
month = '0' + month;
}
if (day.length < 2) {
day = '0' + day;
}
return [month, day, year].join('/');
}
// increment Date with count of months
function incrementDate(date, counter = 0) {
if (isValidDate(start_date_value)) {
var newDate = new Date(date);
newDate.setMonth(newDate.getMonth() + counter);
console.log(formatDate(newDate));
}
}
var start_date_value = "01/01/2019";
incrementDate(start_date_value, 5) ; // 06/01/2019

javascript date validation check

I will get the date of birth value dynamically from users visiting to the site, but when the user will insert birthday value I have to check whether the user is 18 years old or not.
user will insert a value like 18/09/2012 as dd/mm/yyyy format.
var arr = date.split("/"); //date is the value of birth date.
var day = arr[0];
var month = arr[1];
var year = arr[2];
How should I do it?
Take a look at the following question, it will help you calculate the age -
Calculate age in JavaScript
function getAge(dateString)
{
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate()))
{
age--;
}
return age;
}
you may try any of these functions Age Calculation from Date of Birth Using Javascript/Jquery or check this one Calculate age in JavaScript

Problem with comparing dates in javascript

HI,
I have the following code that is supposed to compare two dates:
var d = ($('#day').val());
var m = ($('#month').val() -1);
var y = $('#year').val();
var birthdate = new Date(y,m,d);
alert('birthdate is' + birthdate);
var today = new Date();
alert('today is'+ today);
var diff = (today - birthdate);
years = Math.floor(diff/(1000*60*60*24*365));
alert(years);
It's basically working but I'm interested to see if the date of birth makes the user over 18 or not. So I've tried to put in 30th march 1993 - which would make the user 17. I'm alerting out the birthdate and it gives me back the correct date (mon mar 29 1993 00:00:00 GMT + 0100 BST)....however this is evaluating to 18 (alert(years) in the above code) when it should evaluate to seventeen. It's not until I put in 3rd April 1993 that it evaluates to 17.
Any ideas?
You have to mind leap-years, timezones... before reinventing the wheel, I recommend that you use DateJS.
if((18).years().ago().isBefore(birthdate)) {
// handle underage visitors
}
That's because you forgot the leap years.
These years had 366 days and occur usually every four years, so in any 18 years there are about four days more than 365*18, thus moving the neccessary start date four days ahead.
Probably in this case it is easier to check
if ((nowyear - birthyear > 18)
|| ((nowyear - birthyear == 18)&&(nowmonth - birthmonth > 0))
|| ((nowyear - birthyear == 18)&&(nowmonth == birthmonth)&&(nowday - birthday >= 0)))
// you're 18!
If you're looking for age, why not just go the simple route and deal with years, months, and days?
function findAge( birthday ){
var today = new Date();
var age = today.getFullYears() - birthday.getFullYears();
if( today.getMonth() - birthday.getMonth() < 0 ){
age--;
}
else if( today.getDay() - birthday.getDay() < 0 && today.getMonth() == birthday.getMonth() ){
age--;
}
}
try to take a look at this post

how to check the age of registrant during registration using jquery

I want to filter all the user who can register in my website. how can i filter the age of the registrant using jquery allowing 18 years old and above, but when the age is 13 to 17 years old they can register but they must check checkbox for parental consent. I am using a textbox with mm/dd/yyyy format.
My first thought:
Usage:
var age = getAge(new Date(1984, 7, 31));
function getAge(birthDate) {
var now = new Date();
function isLeap(year) {
return (((year % 4)==0) && ((year % 100)!=0) || ((year % 400)==0));
}
// days since the birthdate
var days = Math.floor((now.getTime() - birthDate.getTime())/1000/60/60/24);
var age = 0;
// iterate the years
for (var y = birthDate.getFullYear(); y <= now.getFullYear(); y++){
var daysInYear = isLeap(y) ? 366 : 365;
if (days >= daysInYear){
days -= daysInYear;
age++;
// increment the age only if there are available enough days for the year.
}
}
return age;
}
Create 3 date objects - their birthday, a 13 year olds birthday and an 18 year olds birthday for them to be 18/13 today.
var input = document.get..
var dobArr = input.value.split("/");
var dob = new Date();
dob.setFullYear(dobArr[2], dobArr[0]-1, dobArr[1]);
var date18 = new Date();
date18.setFullYear(dobArr[2]-18);
var date13 = new Date();
date13.setFullYear(dobArr[2]-13);
if (dob.valueOf() >= date18.valueOf()) {
//i'm at least 18
} else if (dob.valueOf() >= date13.valueOf()) {
//i'm at least 13 but not 18
} else {
//i'm less than 13
}

Categories