having issues running age check JS

  • Zealous
  • Guru
  • Guru
  • User avatar
  • Posts: 1244
  • Loc: Sydney

Post 3+ Months Ago

been playing around with JS today and found this code while looking for snippets and mite be the end of a long day but can't seam to have the code echo on the page i made. there is something simple i am missing i am sure but just wondering if i can get help showing this.

Code: [ Select ]
function checkAge()
{

var today = new Date();
var d = document.getElementById("dob").value;
if (!/\d{4}\-\d{2}\-\d{2}/.test(d))
{ // check valid format
showMessage();
return false;
}

d = d.split("-");
var byr = parseInt(d[0]);
var nowyear = today.getFullYear();
if (byr >= nowyear || byr < 1900)
{ // check valid year
showMessage();
return false;
}
var bmth = parseInt(d[1],10)-1; // radix 10!
if (bmth <0 || bmth >11)
{ // check valid month 0-11
showMessage();
return false;
}
var bdy = parseInt(d[2],10); // radix 10!
var dim = daysInMonth(bmth+1,byr);
if (bdy <1 || bdy > dim)
{ // check valid date according to month
showMessage();
return false;
}

var age = nowyear - byr;
var nowmonth = today.getMonth();
var nowday = today.getDate();
var age_month = nowmonth - bmth;
var age_day = nowday - bdy;
if (age < 18 )
{
alert ("We're sorry but thesite.com won't allow children under 18 years old to login.");
}
else if (age == 18 && age_month <= 0 && age_day <0)
{
alert ("We're sorry but thesite.com won't allow children under 18 years old to login.");
}


}

function showMessage()
{
if (document.getElementById("dob").value != "")
{
alert ("Invalid date format or impossible year/month/day of birth - please re-enter as YYYY-MM-DD");
document.getElementById("dob").value = "";
document.getElementById("dob").focus();
}
}

function daysInMonth(month,year) { // months are 1-12
var dd = new Date(year, month, 0);
return dd.getDate();
}
  1. function checkAge()
  2. {
  3. var today = new Date();
  4. var d = document.getElementById("dob").value;
  5. if (!/\d{4}\-\d{2}\-\d{2}/.test(d))
  6. { // check valid format
  7. showMessage();
  8. return false;
  9. }
  10. d = d.split("-");
  11. var byr = parseInt(d[0]);
  12. var nowyear = today.getFullYear();
  13. if (byr >= nowyear || byr < 1900)
  14. { // check valid year
  15. showMessage();
  16. return false;
  17. }
  18. var bmth = parseInt(d[1],10)-1; // radix 10!
  19. if (bmth <0 || bmth >11)
  20. { // check valid month 0-11
  21. showMessage();
  22. return false;
  23. }
  24. var bdy = parseInt(d[2],10); // radix 10!
  25. var dim = daysInMonth(bmth+1,byr);
  26. if (bdy <1 || bdy > dim)
  27. { // check valid date according to month
  28. showMessage();
  29. return false;
  30. }
  31. var age = nowyear - byr;
  32. var nowmonth = today.getMonth();
  33. var nowday = today.getDate();
  34. var age_month = nowmonth - bmth;
  35. var age_day = nowday - bdy;
  36. if (age < 18 )
  37. {
  38. alert ("We're sorry but thesite.com won't allow children under 18 years old to login.");
  39. }
  40. else if (age == 18 && age_month <= 0 && age_day <0)
  41. {
  42. alert ("We're sorry but thesite.com won't allow children under 18 years old to login.");
  43. }
  44. }
  45. function showMessage()
  46. {
  47. if (document.getElementById("dob").value != "")
  48. {
  49. alert ("Invalid date format or impossible year/month/day of birth - please re-enter as YYYY-MM-DD");
  50. document.getElementById("dob").value = "";
  51. document.getElementById("dob").focus();
  52. }
  53. }
  54. function daysInMonth(month,year) { // months are 1-12
  55. var dd = new Date(year, month, 0);
  56. return dd.getDate();
  57. }
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9090
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Did you ever figure this out? What is the HTML portion that goes with it (the relevant part anyway). Maybe I missed it, but how are you calling the checkAge() function?

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 124 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.