p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Javascript How-To (http://p2p.wrox.com/forumdisplay.php?f=87)
-   -   Over 21 calculator - project (http://p2p.wrox.com/showthread.php?t=27386)

PetraMan March 28th, 2005 05:16 PM

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.

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

var curdate = new Date()
document.write(curdate.getDate())
document.write(".")
document.write(curdate.getMonth() + 1)
document.write(".")
document.write(curdate.getFullYear())
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"); }
else
{alert ("Please enter a valid age"); }
}
</SCRIPT>

</BODY>
</HTML


joefawcett March 29th, 2005 09:44 AM

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.
Code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<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("/");
  try
  {
    var dt =  new Date(arrDtParts[2], (arrDtParts[0] - 1), arrDtParts[1]);
  }
  catch(e)
  {
    return null;
  }
  return dt;
}

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

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;
}

</script>
</head>
<body>
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">
</body>
</html>



--

Joe (Microsoft MVP - XML)


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

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