/    Sign up×
Community /Pin to ProfileBookmark

For Loops – Date of birth

Hi all, bit new to javascript.

Could someone point me in the right direction as to accomplishing this please?

On some forums when you sign up you are asked what your date of birth is with three drop down lists (day // month // year). How do I do this and get it so that it automatically only allows access for anyone who is 18 or older based against the current year date (so as of writing february 19th 2009 would mean the youngest they could sign up would be if their date of birth was on 19/02/1991). etc

From reading other similiar threads I think it would start with:

[CODE]var d = new Date()
var time = d.getYear()
document.writeln(“It is the year “+time+”</p>”);[/CODE]

Thank you for your help

SuperS

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERFeb 19.2009 — See if this helps you collect your thoughts:
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Age Check&lt;/title&gt;
&lt;script type="text/javascript"&gt;
function $(IDS) {
return document.getElementById(IDS).value;
}
function Age(t,n) {
return (n-t)/1000/60/60/24/365;
}
function CheckAge() {
var now = new Date();
var then = new Date($('y'),$('m')-1,$('d'));
alert(now.toDateString()+'n'+then.toDateString()+'n'+Age(then,now));
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
Your birthdate (mm/dd/yyyy):
&lt;input type="text" id="m" value="" size="2"&gt;/
&lt;input type="text" id="d" value="" size="2"&gt;/
&lt;input type="text" id="y" value="" size="2"&gt;
&lt;button onclick="CheckAge()"&gt;Check Age&lt;/button&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@SuperStyle5authorFeb 25.2009 — That's awesome but how would I make it so that if the user is 18 years old or older that the page redirects to a new page and that if they are not 18 or older a message box comes up saying "sorry you are not old enough" etc?
Copy linkTweet thisAlerts:
@JMRKERFeb 25.2009 — See if this variation is something you can use. ?
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Age Check&lt;/title&gt;
&lt;script type="text/javascript"&gt;
var Months = 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec';
function $(IDS) {
return document.getElementById(IDS).value;
}
function Age(t,n) {
var tn = n.toDateString().split(' ');
var tt = t.toDateString().split(' ');
yrs = ((Number(tn[3])-Number(tt[3]))*12);
var mon = (Months.indexOf(tn[1])/4 - Months.indexOf(tt[1])/4);
var day = Number(tn[2]-tt[2]);
var age = (yrs+mon)+(day/30); // 252 months = 21 years
return age;
}
function CheckAge() {
var str = '';
var locn = '';
var now = new Date();
var then = new Date($('y'),$('m')-1,$('d'));
if (Age(then,now) &gt;= 252) {
str = 'Over 21'; <br/>
locn = 'http://www.somewhere.com';
} else {
str = 'You are to young to enter'; <br/>
locn = 'http://www.somewhereElse.com';
}

// remove next line after testing <br/>
alert(str+'nn'+now.toDateString()+'n'+then.toDateString()+'nn'+Age(then,now));
// document.location.href = locn; // redirection code after testing

}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
Your birthdate (mm/dd/yyyy):
&lt;input type="text" id="m" value="" size="2"&gt;/
&lt;input type="text" id="d" value="" size="2"&gt;/
&lt;input type="text" id="y" value="" size="2"&gt;
&lt;button onclick="CheckAge()"&gt;Check Age&lt;/button&gt;
&lt;/body&gt;
&lt;/html&gt;

Change commented lines to redirect instead of alert messages.

NOTE: Not a very secure method to keep people away from a site. :eek:

a) They can lie about their age.

b) They can 'view source' to see what address to use.
×

Success!

Help @SuperStyle5 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...