egolddude
asked on
JavaScript Age Validation not working on MOZILLA
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. getElement ById("DaMo nth").opti ons[docume nt.getElem entById("D aMonth").s electedInd ex].value) ;
var DaDay=parseInt(document.ge tElementBy Id("DaDay" ).options[ document.g etElementB yId("DaDay ").selecte dIndex].va lue);
var DaYear=parseInt(document.g etElementB yId("DaYea r").option s[document .getElemen tById("DaY ear").sele ctedIndex] .value);
age=year-DaYear;
if(DaMonth>month){age--;}
else{if(DaDay>=date){age-- ;}}
if(DaMonth==00){alert("Ple ase enter your birth month!");return false;}
else if(DaDay==00){alert("Pleas e enter your birth date!");return false;}
else if(DaYear==00){alert("Plea se 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.getElementByI d("agree") .checked){ alert('Che ck 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>
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.
var DaDay=parseInt(document.ge
var DaYear=parseInt(document.g
age=year-DaYear;
if(DaMonth>month){age--;}
else{if(DaDay>=date){age--
if(DaMonth==00){alert("Ple
else if(DaDay==00){alert("Pleas
else if(DaYear==00){alert("Plea
else if(age<21){alert("Enter our site only if you're 21");location.replace("http://www.google.com/");
else if(!document.getElementByI
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>
Also the if's are not fool-proof.
Check this:
if(DaMonth>month){age--;}
else{if(DaMonth==month&&Da Day>=date) {age--;}}
Check this:
if(DaMonth>month){age--;}
else{if(DaMonth==month&&Da
ASKER
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.val ue, 10);
var DaYear=parseInt(f.DaYear.v alue);
age=year-DaYear;
if(DaMonth>month){age--;}
else{if(DaMonth==month&&Da Day>=date) {age--;}}
if(DaMonth==00){alert("Ple ase enter your birth month!");return false;}
else if(DaDay==00){alert("Pleas e enter your birth date!");return false;}
else if(DaYear==00){alert("Plea se 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.getElementByI d("agree") .checked){ alert('Che ck 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 ...
<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
var DaDay=parseInt(f.DaDay.val
var DaYear=parseInt(f.DaYear.v
age=year-DaYear;
if(DaMonth>month){age--;}
else{if(DaMonth==month&&Da
if(DaMonth==00){alert("Ple
else if(DaDay==00){alert("Pleas
else if(DaYear==00){alert("Plea
else if(age<21){alert("Enter our site only if you're 21");location.replace("http://www.google.com/");
else if(!document.getElementByI
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 ...
What values do you have in DaYear ? 1960 or only 60?
ASKER
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>
<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>
Put the 00 within qoutes
if(DaMonth=="00"){.....}
if(DaMonth=="00"){.....}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.val ue, 10);
var DaYear=parseInt(f.DaYear.v alue);
age=year-DaYear;
if(DaMonth>month){age--;}
else{if(DaMonth==month&&Da Day>=date) {age--;}}
if(DaMonth=="00"){alert("P lease enter your birth month!");return false;}
else if(DaDay=="00"){alert("Ple ase enter your birth date!");return false;}
else if(DaYear=="00"){alert("Pl ease 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.getElementByI d("agree") .checked){ alert('Che ck the agree box to enter our site!');return false;}
else{return true;}
}
</script>
And ...
you got the points ...
@"~"@
<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
var DaDay=parseInt(f.DaDay.val
var DaYear=parseInt(f.DaYear.v
age=year-DaYear;
if(DaMonth>month){age--;}
else{if(DaMonth==month&&Da
if(DaMonth=="00"){alert("P
else if(DaDay=="00"){alert("Ple
else if(DaYear=="00"){alert("Pl
else if(age<21){alert("Enter our site only if you're 21");location.replace("http://www.google.com/");
else if(!document.getElementByI
else{return true;}
}
</script>
And ...
you got the points ...
@"~"@
<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
var DaDay=parseInt(f.DaDay.val
var DaYear=parseInt(f.DaYear.v
age=year-DaYear;
if(DaMonth>month){age--;}
else{if(DaDay>=date){age--
if(DaMonth==00){alert("Ple
else if(DaDay==00){alert("Pleas
else if(DaYear==00){alert("Plea
else if(age<21){alert("Enter our site only if you're 21");location.replace("http://www.google.com/");
else if(!document.getElementByI
else{return true;}
}
</script>
</head>
<body>
<form name="Only21" action="mainsite.html" onsubmit="return Age21Min(this)">