JDidit
asked on
can't get the age validation to work
I am having an issue getting the age code to validate. The user needs to be over the age of 18 to submit the order. The problem I am having is no matter what year I put in, the user is considered over the age 18. If i switch the "<" to a ">", the alert I get no matter what year is chosen is the "You are to young to submit an order" error.
<html>
<body>
<form name="webForm" method="post" action="" onSubmit="return frmSubmit();">
<p>
Full Name:<input type="text" name="custname" id="custname" value=""/>
<br/>
Address:
<br/>
<textarea name="address" rows="3" cols="40" id="">
</textarea>
<br/>
<label for="gender">Sex:</label>
<input type="radio" name="gender" value="male" />Male
<input type="radio" name="gender" value="male" />Female
<br/>
Birthday:
<br/>
<label for="bday">Day:</label>
<select name="bday" id="bday">
<option value=""></option>
<option value='01'>01</option>
<option value='31'>31</option>
</select>
<label for="bmonth">Month:</label>
<select name="bmonth" id="bmonth">
<option value=""></option>
<option value='01'>1</option>
<option value='12'>12</option>
</select>
<label for="byear">Year:</label>
<select name="byear" id="byear">
<option value=""></option>
<option value='1'>1950</option>
<option value='59'>2008</option>
</select>
<br/>
<br/>
<input type="checkbox" name="newsl" value"1">Send me Newsletter</p>
<input type="hidden" name="order" value="order"/>
<input type="button" value=" Submit " onclick="frmSubmit();"/>
<input type="reset"/></p>
</form>
<font size="+2"><b>
<script type="text/javascript">
function frmSubmit()
{
var minutes = 1000*60;
var hours = minutes*60;
var days = hours*24;
var years = days*365;
var d = new Date();
var t = d.getTime();
var y = t/years; /* above var's calculate time converted to years since 01JAN1970 */
var xyear = parseInt(document.forms[0].byear.value); /*pulls the value of byear*/
var yyear = xyear-1970; /*subtracts byear from 1970.*/
var zyear = y-yyear;
/* example math if the user is 30:
1978-1970=8; as of 16Jun08 coverted years since 1970 is 38;
38-8=30
example math is user is 12:
1996-1970=26;
38-26=12
example math is user is 58:
1950-1970=-20
38-(-20)=58 */
if (document.forms[0].custname.value.length < 3 )
{
alert ("Please fill in the 'Full Name' box.");return false;
}
if ((document.forms[0].gender[0].checked) || (document.forms[0].gender[1].checked))
{
radio_choice=true;
}
else
{
alert("Please select a Gender.");
return false;
}
if (zyear > 18)
{
alert("You are to young to submit an order!");
return false;
}
else
{
alert("Order Submitted");
return true;
}
}
</script>
</body>
</html>
<select name="byear" id="byear">
<option value=""></option>
<option value='1950'>1950</option>
<option value='2008'>2008</option>
</select>
<script type="text/javascript">
function frmSubmit()
{
var minutes = 1000*60;
var hours = minutes*60;
var days = hours*24;
var years = days*365;
var d = new Date();
var t = d.getTime();
var y = t/years; /* above var's calculate time converted to years since 01JAN1970 */
var xyear = parseInt(document.forms[0].byear[document.forms[0].byear.selectedIndex].value); /*pulls the value of byear*/
var zyear = (new Date()).getYear() + 1900;
zyear = zyear - xyear;
/* example math if the user is 30:
1978-1970=8; as of 16Jun08 coverted years since 1970 is 38;
38-8=30
example math is user is 12:
1996-1970=26;
38-26=12
example math is user is 58:
1950-1970=-20
38-(-20)=58 */
if (document.forms[0].custname.value.length < 3 )
{
alert ("Please fill in the 'Full Name' box.");return false;
}
if ((document.forms[0].gender[0].checked) || (document.forms[0].gender[1].checked))
{
radio_choice=true;
}
else
{
alert("Please select a Gender.");
return false;
}
if (zyear < 18)
{
alert("You are to young to submit an order!");
return false;
}
else
{
alert("Order Submitted");
return true;
}
}
</script>
@neeraj523 - sorry, wish there was an alert to warn that a post has been posted
@JDidit - neerag523's solution is more thorough than mine because it takes into account the month and dates, so ignore my post
@JDidit - neerag523's solution is more thorough than mine because it takes into account the month and dates, so ignore my post
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
I had to reverse the code for
[code]
if (zyear > 18)
[/code]
to
[code]
if (zyear <18)
[/code]
and it work perfectly. Thank you!!.
[code]
if (zyear > 18)
[/code]
to
[code]
if (zyear <18)
[/code]
and it work perfectly. Thank you!!.
You are welcome!
Dear JDidit
Have you tried my solution ?? did it give any issue ??
Have you tried my solution ?? did it give any issue ??
ASKER
Neeraj523,
I did try it, and it works great too!! Thanks for your input. It was not working for me last night night for some reason. Is it possible that isn't compatable with FF, that is what I was using last night? I am using IE 6 right now it it worked just fine.
I did try it, and it works great too!! Thanks for your input. It was not working for me last night night for some reason. Is it possible that isn't compatable with FF, that is what I was using last night? I am using IE 6 right now it it worked just fine.
try this
Open in new window