/    Sign up×
Community /Pin to ProfileBookmark

OK.. My girlfriend is taking Javascript in school. She has some code written but for some reason it won’t work.. This is her first try at javascript and is taking an online class so there isn’t much help.. She doesn’t like to use forums but I’ve found them to be very helpful.

The project is:

Create a pop up window asking for age verification.
If under 21 direct to under 21 webpage.
If over 21 direct to over 21 webpage.
If over 21 and Birthday is current date Popup Happy Birthday Window and Direct to over 21 webpage.

Below is the code writen so far. It’s not a pop up window yet and won’t direct to webpages. Any Advice.

<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″/>
<title>JavaScript Exercise</title>
</head>

<script type=”text/javascript”>

//global variables
var legalAge = 21;
var today = new Date();
var legalBday = today – legalAge;
var legalBdayYear = legalBday.getFullYear();
var legalBdayMonth = legalBday.getMonth()+1;
var legalBdayDate = legalBday.getDate();

//user prompt
function bday_prompt()
{
var bday = prompt(“Enter your birthday in MMDDYYYY format. (Example: August 17, 1976 = 08171976.)”,””)
var bdayYear = bday.charAt(4) + bday.charAt(5) + bday.charAt(6) + bday.charAt(7);
var bdayMonth = bday.charAt(0) + bday.charAt(1);
var bdayDate = bday.charAt(2) + bday.charAt(3);
if (bday != “”)
{
over21()
}
}

//validate input
function validateBday(bday)
{
if(bday.length != 8)
{
alert(“Please enter your birthday in MMDDYYYY format. (Example: August 17, 1976 = 08171976”)
}
}

//happy birthday!
function happybirthday()
if (bdayDate = legalBdayDate && bdayMonth = legalBdayMonth)
{
var p=window.createPopup();
var pbody=p.document.body;
pbody.style.backgroundColor=”SkyBlue”;
pbody.style.border=”solid black 1px”;
pbody.innerHTML=”<img src=’http://www.clipart-graphics.net/gallery/clipart/Cartoon/Birthday/Bday3.jpg’><br><br><font face=’verdana’ size=’5′>Happy Birthday!</font><br><br><font face=’verdana’ size=’1′>(Click outside popup to close and party!)</font>”;
p.show(100,100,245,300,document.body);
}

//over 21?
function over21()
validateBday(bday)
happybirthday()
if (bdayYear < legalBdayYear)
{
document.write(window.location = “ex3/over21.htm”);
}
else if (bdayYear = legalBdayYear && bdayMonth < legalBdayMonth)
{
document.write(window.location = “ex3/over21.htm”);
}
else if (bdayYear = legalBdayYear && bdayMonth = legalBdayMonth && bdayDate <= legalBdayDate)
{
document.write(window.location = “ex3/over21.htm”);
}
else
{
document.write(window.location = “ex3/under21.htm”);
}

</script>

<body>
<form name=”BDay” method=”post” action=”validateBday”>
<p>Please enter your birthday in MMDDYYYY format. (Example: August 17, 1976 = 08171976</p>
<p>Your BirthDate:
<input type=”text” name=”bday”>
</p>
<p>Submit
<input type=”submit” name=”Submit” value=”Submit”>
</p>
<p>&nbsp; </p>
</form>
</body>
</html>

Thanks In Advance.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@NedalsMar 02.2007 — ... She doesn't like to use forums but I've found them to be very helpful.[/QUOTE]Convince her!. After a couple of posts she will get to love this one. And she'll learn a lot. ?

Do you really mean pop-up or just a prompt as used with the code you posted.

use an 'onload="bday_prompt()" event to cause the prompt to appear.

Keep the code simple... (too much in your example)

In the bday_prompt function

Get the entered date, compare against today.

if (diff>21) { location.href = over21.html }

else if (diff<21) { location.href = under21.html }

else { location.href = happy_bday.html }

on the happy_bday.html, add a redirect after say 5 secs to over21.html.

No need for any forms or submits on the stating page.

(You should end up with under 20 lines of javascript code.)
Copy linkTweet thisAlerts:
@YELLOWCAKEauthorMar 07.2007 — The problem she is having is compairing the date of the user to the date function. The teacher want's the date entered as Month, Day, Year.. I think the date function gives the date as year, month, date.. how to you calculate it to tell if you get the age 21, above or below? It seems we are having trouble parsing the data?

Thanks.
×

Success!

Help @YELLOWCAKE 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,
)...