Solved

JavaScript Age Validation not working on MOZILLA

Posted on 2006-11-06
8
484 Views
Last Modified: 2008-01-09
I have this below JavaScript on one of my page to only allowed visitors with minimal age 21 to get in ... The problem is, this codes is NOT WORKING on MOZILLA ... :(
Would anybody help me fix the codes so MOZILLA will accept it .. or any suggestion?

<head>
<script type="text/javascript">
function Age21Min(f){

var dob=new Date();
var date=dob.getDate();
var month=dob.getMonth();
var year=dob.getYear();

var DaMonth=parseInt(document.getElementById("DaMonth").options[document.getElementById("DaMonth").selectedIndex].value);
var DaDay=parseInt(document.getElementById("DaDay").options[document.getElementById("DaDay").selectedIndex].value);
var DaYear=parseInt(document.getElementById("DaYear").options[document.getElementById("DaYear").selectedIndex].value);

age=year-DaYear;

if(DaMonth>month){age--;}
else{if(DaDay>=date){age--;}}

if(DaMonth==00){alert("Please enter your birth month!");return false;}
else if(DaDay==00){alert("Please enter your birth date!");return false;}
else if(DaYear==00){alert("Please enter your birth year!");return false;}
else if(age<21){alert("Enter our site only if you're 21");location.replace("http://www.google.com/");return false;}
else if(!document.getElementById("agree").checked){alert('Check the agree box to enter our site!');return false;}
else{return true;}

}
</script>
</head>

<body>
<form name="Only21" action="mainsite.html" onsubmit="return Age21Min(this)">
<input .. blah blah blah ..>
<input .. blah blah blah ..>
</body>
0
Comment
Question by:egolddude
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 17885262
Check this:

<head>
<script type="text/javascript">
function Age21Min(f){

var dob=new Date();
var date=dob.getDate();
var month=dob.getMonth();
var year=dob.getYear();

var DaMonth=parseInt(f.DaMonth.value, 10);
var DaDay=parseInt(f.DaDay.value, 10);
var DaYear=parseInt(f.DaYear.value);

age=year-DaYear;

if(DaMonth>month){age--;}
else{if(DaDay>=date){age--;}}

if(DaMonth==00){alert("Please enter your birth month!");return false;}
else if(DaDay==00){alert("Please enter your birth date!");return false;}
else if(DaYear==00){alert("Please enter your birth year!");return false;}
else if(age<21){alert("Enter our site only if you're 21");location.replace("http://www.google.com/");return false;}
else if(!document.getElementById("agree").checked){alert('Check the agree box to enter our site!');return false;}
else{return true;}

}
</script>
</head>

<body>
<form name="Only21" action="mainsite.html" onsubmit="return Age21Min(this)">


0
 
LVL 63

Expert Comment

by:Zvonko
ID: 17885280
Also the if's are not fool-proof.
Check this:

if(DaMonth>month){age--;}
else{if(DaMonth==month&&DaDay>=date){age--;}}


0
 

Author Comment

by:egolddude
ID: 17885873
Okay 've been trying with yours:

<script type="text/javascript">
function Age21Min(f){

var dob=new Date();
var date=dob.getDate();
var month=dob.getMonth();
var year=dob.getYear();

var DaMonth=parseInt(f.DaMonth.value, 10);
var DaDay=parseInt(f.DaDay.value, 10);
var DaYear=parseInt(f.DaYear.value);

age=year-DaYear;

if(DaMonth>month){age--;}
else{if(DaMonth==month&&DaDay>=date){age--;}}

if(DaMonth==00){alert("Please enter your birth month!");return false;}
else if(DaDay==00){alert("Please enter your birth date!");return false;}
else if(DaYear==00){alert("Please enter your birth year!");return false;}
else if(age<21){alert("Enter our site only if you're 21");location.replace("http://www.google.com/");return false;}
else if(!document.getElementById("agree").checked){alert('Check the agree box to enter our site!');return false;}
else{return true;}

}
</script>

PROBLEM is:
On MOZILLA, the problem is still the same ... Yes it validates Month, Date, and Year if each has no value (or value is 00) (from a drop down box) ... but on the last checked (Year) it will pop "Enter our site only if you're 21" no matter what year I choosen.

Any solution?  Please ...
0
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
LVL 63

Expert Comment

by:Zvonko
ID: 17885889
What values do you have in DaYear ? 1960 or only 60?

0
 

Author Comment

by:egolddude
ID: 17885985
For DaYear .. it is in 4 digits (except the "00" value) ... here's it the excerpt:

<select id="DaYear">
<option value="00">Year</option>
<option value="1990">1990</option>
<option value="1989">1989</option>
<option value="1988">1988</option>
<option value="1987">1987</option>
<option value="1986">1986</option>
<option value="1985">1985</option>
<option value="1984">1984</option>
<option value="1983">1983</option>
<option value="1982">1982</option>
<option value="1981">1981</option>
</select>
0
 
LVL 17

Expert Comment

by:gops1
ID: 17886114
Put the 00 within qoutes

if(DaMonth=="00"){.....}
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 250 total points
ID: 17887188
Use instead of this:
var year=dob.getYear();

Better this:
var year=dob.getFullYear();

0
 

Author Comment

by:egolddude
ID: 17887575
Okay ... I've changed the codes to:

<script type="text/javascript">
function Age21Min(f){

var dob=new Date();
var date=dob.getDate();
var month=dob.getMonth();
var year=dob.getFullYear();

var DaMonth=parseInt(f.DaMonth.value, 10);
var DaDay=parseInt(f.DaDay.value, 10);
var DaYear=parseInt(f.DaYear.value);

age=year-DaYear;

if(DaMonth>month){age--;}
else{if(DaMonth==month&&DaDay>=date){age--;}}

if(DaMonth=="00"){alert("Please enter your birth month!");return false;}
else if(DaDay=="00"){alert("Please enter your birth date!");return false;}
else if(DaYear=="00"){alert("Please enter your birth year!");return false;}
else if(age<21){alert("Enter our site only if you're 21");location.replace("http://www.google.com/");return false;}
else if(!document.getElementById("agree").checked){alert('Check the agree box to enter our site!');return false;}
else{return true;}

}
</script>

And ...

you got the points ...

@"~"@
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question