Solved

can't get the age validation to work

Posted on 2008-06-16
8
1,479 Views
Last Modified: 2008-06-17
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>

Open in new window

0
Comment
Question by:JDidit
[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
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 22

Expert Comment

by:neeraj523
ID: 21799171
Hello

try this
<html>
<body>
 
<form name="webForm" method="post" action="" onSubmit="return frmSubmit();">
<p>
Full Name:<input type="text" name="custname" id="custname" value="test"/>
<br/>
Address:
<br/>
<textarea name="address" rows="3" cols="40" id="">test
</textarea>
<br/>
<label for="gender">Sex:</label>
  <input type="radio" name="gender" value="male" CHECKED/>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' SELECTED>31</option>
  </select>
  <label for="bmonth">Month:</label>
  <select name="bmonth" id="bmonth">
    <option value=""></option>
    <option value='01'>1</option>
    <option value='12' SELECTED>12</option>
  </select>
  <label for="byear">Year:</label>
  <select name="byear" id="byear">
    <option value=""></option>
    <option value='1950' SELECTED>1950</option>
    <option value='2008'>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>
 
<script type="text/javascript">
function frmSubmit()
{
 var bDay = document.forms[0].byear.value + "/" + document.forms[0].bmonth.value + "/" + document.forms[0].bday.value
 
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 (dateDiff(bDay, getNow()) < 18)
 {
   alert("You are to young to submit an order!");
   return false;
 }
else
 {
   alert("Order Submitted");
   return true;
 }
}
 
function getNow()
{
	var d = new Date();
	return d.getYear() + "/" + d.getMonth() + "/" + d.getDate();
}
 
function dateDiff(strDate1,strDate2)
{
    datDate1= Date.parse(strDate1);
    datDate2= Date.parse(strDate2);
    dur = (datDate2-datDate1)/(365*24*60*60*1000)
	return dur
}
 
</script>
 
</body>
</html>

Open in new window

0
 
LVL 6

Expert Comment

by:ysfx
ID: 21799178

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

Open in new window

0
 
LVL 6

Expert Comment

by:ysfx
ID: 21799197
@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
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 21799252
Try this:
<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='56'>2006</option>
    <option value='57'>2007</option>
    <option value='58'>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()
{
	//get current date
	var now = new Date();
	//set it back by 18 years
	now.setYear(now.getFullYear());	 
	//determine user-specified date
	var specifiedDate = new Date();
	specifiedDate.setMonth(parseInt(document.forms[0].bmonth[document.forms[0].bmonth.selectedIndex].text)-1)
	specifiedDate.setDate(parseInt(document.forms[0].bday[document.forms[0].bday.selectedIndex].text));
	specifiedDate.setYear(parseInt(document.forms[0].byear[document.forms[0].byear.selectedIndex].text));
	if(isNaN( specifiedDate.valueOf()) )
	{
		alert("Please provide your birthdate");
		return false;
	}
var zyear = now.getFullYear() - specifiedDate.getFullYear(); 
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>

Open in new window

0
 

Author Comment

by:JDidit
ID: 21799315
I had to reverse the code for
[code]
 if (zyear > 18)
[/code]
 to  
[code]
if (zyear <18)
[/code]
 and it work perfectly.  Thank you!!.
0
 
LVL 82

Expert Comment

by:hielo
ID: 21799343
You are welcome!
0
 
LVL 22

Expert Comment

by:neeraj523
ID: 21799635
Dear JDidit

Have you tried my solution ?? did it give any issue ??
0
 

Author Comment

by:JDidit
ID: 21807893
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.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

732 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