Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 28th, 2005, 05:16 PM
Registered User
Join Date: Mar 2005
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Over 21 calculator - project

I am trying to create a JavaScript "over 21 calculator". It's a school project.

I am not understanding how to take User Input Bday and subtract it from my curdate to get the resulting over 21 or not. And then have the script prompt with either Not old enough to drink or Let's party.

I'm a Novice so I'll explain what I want this to do. User clicks on Submit Button, Script prompts for User Input of a Birthday mm/dd/yyyy. Then script takes computer's Current Date and subtracts entered User Bday from Current Date to figure out if they are 21 or not. Then script prompts Lets part or Not old enough.

Yes Mr. MVP I have already looked at the archives and did not find what I need. And I don't know what to look for in the book JavaScript 2nd edition.

Any help would be Greatly Appreciated!! I'm stressing over not knowing.

<TITLE>Legal Drinking Age Meter </TITLE>
 <input type="button" value="Submit" onClick="legalAge()">
<script language="JavaScript" type="text/JavaScript">

var curdate = new Date()
document.write(curdate.getMonth() + 1)
function legalAge()
var ageString;
var legalAge=21;
var ageNum;
       ageString = prompt ("Please enter your age", " ");
       ageNum = parseInt(ageString);
document.write(ageString + "<br>");
if (ageNum >= legalAge)
{alert ("Let's Party!");}
else if (ageNum < legalAge)
{alert ("Sorry! You're too young to drink!");}
else if (ageNum < 0)
{alert ("Please enter a valid age"); }
{alert ("Please enter a valid age"); }


Reply With Quote
  #2 (permalink)  
Old March 29th, 2005, 09:44 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

Here's something to get you started. The date validation is not perfect by any means, as I said before it's a difficult topic. You can easily improve it though by adding to the validateDate function and the other code will not need changing. In a real world application I would use a calendar control to make sure the user entered a valid adate. This also avoids worrying about the fact the format of dates varies form place to place.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<title>Calculate Age</title>
<script type="text/javascript">
function validateDate(DateString)
  var oRE = /^\d\d\/\d\d\/\d\d\d\d$/;
  if (oRE.test(DateString))
    var dt = getJsDate(DateString);
    return (dt != null);
  return false;

function getJsDate(USDateString)
  var arrDtParts = USDateString.split("/");
    var dt =  new Date(arrDtParts[2], (arrDtParts[0] - 1), arrDtParts[1]);
    return null;
  return dt;

function tryCalculateAge(Textbox)
  if (validateDate(Textbox.value))
    var iAge = calculateAge(getJsDate(Textbox.value));
    alert("Invalid date entered. You must submit a date in the format 'mm/dd/yyyy'.");

function calculateAge(Birthday)
  var dtNow = new Date();
  var iYears = dtNow.getFullYear() - Birthday.getFullYear();
  var iBirthMonth = Birthday.getMonth();
  var iNowMonth = dtNow.getMonth()
  if ( iNowMonth > iBirthMonth)
    return iYears;
  if (iNowMonth < iBirthMonth)
    return iYears - 1;
  var iBirthDate = Birthday.getDate();
  var iNowDate = dtNow.getDate();
  if (iNowDate > iBirthDate)
    return iYears;
  if (iNowDate < iBirthDate)
    return iYears - 1;
  alert("Happy Birthday!");
  return iYears;

Enter Birthday (dd/mm/yyyy):#160;<input type="text" size="12" maxlength="10" id="txtBirthday"><br>
<input type="button" onclick="tryCalculateAge(document.getElementById('txtBirthday'));" value="Show Age">


Joe (Microsoft MVP - XML)
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

All times are GMT -4. The time now is 09:56 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.