1 Replies - 6010 Views - Last Post: 06 March 2010 - 10:13 AM

#1 k3nnt0ter0   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 188
  • Joined: 16-January 10

Leap year validation

Posted 06 March 2010 - 04:40 AM

It's me again and I'm almost done with my program. The last problem that I encountered is if I selected a a month and day, it keep alerting that there is no february 29 in year ++++. I encountered this problem after including this codes
	if (document.form1.month.value == 2 && document.form1.date.value == 29 && document.form1.year.value % 4 == 0 || document.form1.year.value % 100 != 0 || document.form1.year.value % 400 == 0)
		{
			alert("There is no February 29 in "+document.form1.year.value);
		}


How can I prevent the alert box from popping up? Any help will do.
Here's my complete code
<html>
        <head>
                <title>Zodiac Sign</title>
<script language="Javascript">

function zodiac()
{
//Astrological Zodiac Sign

                if (document.form1.month.value == 1 && document.form1.date.value <=19)
                        {
                                document.form1.sign.value = "Capricorn";
                        }
                if (document.form1.month.value == 1 && document.form1.date.value >=20)
                        {
                                document.form1.sign.value = "Aquarius";
                        }
                if (document.form1.month.value == 2 && document.form1.date.value <=18) 
                        {
                                document.form1.sign.value = "Aquarius";
                        }
                if (document.form1.month.value == 2 && document.form1.date.value >=19)
                        {
                                document.form1.sign.value = "Pisces";
                        }
                if (document.form1.month.value == 3 && document.form1.date.value <=20)
                        {
                                document.form1.sign.value = "Pisces";
                        }
                if (document.form1.month.value == 3 && document.form1.date.value >=21) 
                        {
                                document.form1.sign.value = "Aries";
                        }
                if (document.form1.month.value == 4 && document.form1.date.value <=20)
                        {
                                document.form1.sign.value = "Aries";
                        }
                if (document.form1.month.value == 4 && document.form1.date.value >=21)
                        {
                                document.form1.sign.value = "Taurus";
                        }
                if (document.form1.month.value == 5 && document.form1.date.value <=20)
                        {
                                document.form1.sign.value = "Taurus";
                        }
                if (document.form1.month.value == 5 && document.form1.date.value >=21)
                        {
                                document.form1.sign.value = "Gemini";
                        }
                if (document.form1.month.value == 6 && document.form1.date.value <=20)
                        {
                                document.form1.sign.value = "Gemini";
                        }
                if (document.form1.month.value == 6 && document.form1.date.value >=21)
                        {
                                document.form1.sign.value = "Cancer";
                        }
                if (document.form1.month.value == 7 && document.form1.date.value <=21)
                        {
                                document.form1.sign.value = "Cancer";
                        }
                if (document.form1.month.value == 7 && document.form1.date.value >=22)
                        {
                                document.form1.sign.value = "Leo";
                        }
                if (document.form1.month.value == 8 && document.form1.date.value <=21)
                        {
                                document.form1.sign.value = "Leo";
                        }
                if (document.form1.month.value == 8 && document.form1.date.value >=22)
                        {
                                document.form1.sign.value = "Virgo";
                        }
                if (document.form1.month.value == 9 && document.form1.date.value <=21)
                        {
                                document.form1.sign.value = "Virgo";
                        }
                if (document.form1.month.value == 9 && document.form1.date.value >=22)
                        {
                                document.form1.sign.value = "Libra";
                        }
                if (document.form1.month.value == 10 && document.form1.date.value <=21)
                        {
                                document.form1.sign.value = "Libra";
                        }
                if (document.form1.month.value == 10 && document.form1.date.value >=22)
                        {
                                document.form1.sign.value = "Scorpio";
                        }
                if (document.form1.month.value == 11 && document.form1.date.value <=21)
                        {
                                document.form1.sign.value = "Scorpio";
                        }
                if (document.form1.month.value == 11 && document.form1.date.value >=22)
                        {
                                document.form1.sign.value = "Sagittarius";
                        }
                if (document.form1.month.value == 12 && document.form1.date.value <=20)
                        {
                                document.form1.sign.value = "Sagittarius";
                        }
                if (document.form1.month.value == 12 && document.form1.date.value >=21)
                        {
                                document.form1.sign.value = "Capricorn";
                        }
//Chinse Zodiac Sign
                if ((document.form1.year.value=='1984') || (document.form1.year.value=='1996') || (document.form1.year.value=='2008'))
                        {
                                document.form1.csign.value = "Rat";
                        }
                if ((document.form1.year.value=='1985') || (document.form1.year.value=='1997') || (document.form1.year.value=='2009'))
                        {
                                document.form1.csign.value = "Ox";
                        }
                if ((document.form1.year.value=='1986') || (document.form1.year.value=='1998') || (document.form1.year.value=='2010'))
                        {
                                document.form1.csign.value = "Tiger";
                        }
                if ((document.form1.year.value=='1987') || (document.form1.year.value=='1999') || (document.form1.year.value=='2011'))
                        {
                                document.form1.csign.value = "Rabbit";
                        }
                if ((document.form1.year.value=='1988') || (document.form1.year.value=='2000') || (document.form1.year.value=='2012'))
                        {
                                document.form1.csign.value = "Dragon";
                        }
                if ((document.form1.year.value=='1989') || (document.form1.year.value=='2001') || (document.form1.year.value=='2013'))
                        {
                                document.form1.csign.value = "Snake";
                        }
                if ((document.form1.year.value=='1990') || (document.form1.year.value=='2002') || (document.form1.year.value=='2014'))
                        {
                                document.form1.csign.value = "Horse";
                        }
                if ((document.form1.year.value=='1991') || (document.form1.year.value=='2003') || (document.form1.year.value=='2015'))
                        {
                                document.form1.csign.value = "Ram";
                        }
                if ((document.form1.year.value=='1980') || (document.form1.year.value=='1992') || (document.form1.year.value=='2004') || (document.form1.year.value=='2016'))
                        {
                                document.form1.csign.value = "Monkey";
                        }
                if ((document.form1.year.value=='1981') || (document.form1.year.value=='1993') || (document.form1.year.value=='2005'))
                        {
                                document.form1.csign.value = "Rooster";
                        }
                if ((document.form1.year.value=='1982') || (document.form1.year.value=='1994') || (document.form1.year.value=='2006'))
                        {
                                document.form1.csign.value = "Dog";
                        }
                if ((document.form1.year.value=='1983') || (document.form1.year.value=='1995') || (document.form1.year.value=='2008'))
                        {
                                document.form1.csign.value = "Pig";
                        }
		if (document.form1.month.value == 4 && document.form1.date.value == 31)
			{
				document.form1.sign.value = "";
                                document.form1.csign.value = "";
			}
	        if (document.form1.month.value == 6 && document.form1.date.value == 31)
			{
				document.form1.sign.value = "";
                                document.form1.csign.value = "";
			}
	        if (document.form1.month.value == 9 && document.form1.date.value == 31)
			{
				document.form1.sign.value = "";
                                document.form1.csign.value = "";
			}
	        if (document.form1.month.value == 11 && document.form1.date.value == 31)
			{
				document.form1.sign.value = "";
                                document.form1.csign.value = "";
			}
                if (document.form1.month.selectedIndex == "x" || document.form1.date.selectedIndex == "y" || document.form1.year.selectedIndex == "z")
                return;
}

function validate() 
{

	if (document.form1.month.value == 2 && document.form1.date.value == 29 && document.form1.year.value % 4 == 0 || document.form1.year.value % 100 != 0 || document.form1.year.value % 400 == 0)
		{
			alert("There is no February 29 in "+document.form1.year.value);
		}
        if (document.form1.month.value == 2 && document.form1.date.value > 29)
                {
                        alert("There are only a maximum of 29 days in February.");
                }
        if (document.form1.month.value == 4 && document.form1.date.value == 31)
                {
                        alert("There are only 30 days in April."); 
                }
        if (document.form1.month.value == 6 && document.form1.date.value == 31)
                {
                        alert("There are only 30 days in June."); 
                }
        if (document.form1.month.value == 9 && document.form1.date.value == 31)
                {
                        alert("There are only 30 days in September.");
                }
        if (document.form1.month.value == 11 && document.form1.date.value == 31)
                {
                        alert("There are only 30 days in November."); 
                }
        else
                {
                return true;
                }
}
</script>
<body>
<form name="form1">
<center>
<br><br>
<table border="0" bgcolor="b0a2ff" cellspacing="0" cellpadding="1">
        <tr>
                <td>
                        <font size="2" face="Trebuchet MS"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Month&nbsp;</b></font>
                </td>
                <td>
                        <font size="2" face="Trebuchet MS"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;</b></font>
                </td>
                <td>
                        <font size="2" face="Trebuchet MS"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Year</b>&nbsp;</b></font>
                </td>
                <td>
                        <font size="2" face="Trebuchet MS"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Zodiac Sign&nbsp;&nbsp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Chinese Zodiac</b></font>
                </td>
        </tr>
        <tr>
                <td>
                        &nbsp;<select name="month">
                                <option value="x" selected>Select&nbsp;</option>
                                <option value="1">January</option><option value="2">February</option><option value="3">March</option>
                                <option value="4">April</option><option value="5">May</option><option value="6">June</option><option value="7">July</option>
                                <option value="8">August</option><option value="9">September</option><option value="10">October</option><option value="11">November</option>
                                <option value="12">December</option>
                        </select>
                </td>
                <td>
                        &nbsp;<select name="date" onchange="validate()">
                                <option value="y" selected>Select&nbsp;</option>
                                <option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option>
                                <option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option>
                                <option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option>
                                <option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option>
                                <option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option>
                                <option value="23">23</option><option value="24">24</option><option value="25">25</option><option value="26">26</option>
                                <option value="27">27</option><option value="28">28</option><option value="29">29</option><option value="30">30</option>
                                <option value="31">31</option>
                        </select>
                </td>
                <td>
                        &nbsp;<select name="year" onchange="validate()">
                                <option value = 'z' selected>Select&nbsp;</option>
				<option value = '2016'>2016</option><option value = '2015'>2015</option><option value = '2014'>2014</option><option value = '2013'>2013</option><option value = '2012'>2012</option><option value = '2011'>2011</option>
                                <option value = '2010'>2010</option><option value = '2009'>2009</option><option value = '2008'>2008</option><option value = '2007'>2007</option><option value = '2006'>2006</option><option value = '2005'>2005</option><option value = '2004'>2004</option><option value = '2003'>2003</option><option value = '2002'>2002</option><option value = '2001'>2001</option>
                                <option value = '2000'>2000</option><option value = '1999'>1999</option><option value = '1998'>1998</option><option value = '1997'>1997</option><option value = '1996'>1996</option><option value = '1995'>1995</option><option value = '1994'>1994</option><option value = '1993'>1993</option><option value = '1992'>1992</option><option value = '1991'>1991</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>
                                <option value = '1980'>1980
                        </select>
                </td>
                <td>
                        &nbsp;<input name="sign" size="12">
                        &nbsp;<input name="csign" size="12">
                        &nbsp;<input type="button" value="Submit" onclick="zodiac()">
                        &nbsp;<input type="reset" value="Reset" onclick="zodiac()">
                </td>
        </tr>
</table>
</center>
</form>
</body>
</html>


Is This A Good Question/Topic? 0
  • +

Replies To: Leap year validation

#2 JMRKER   User is offline

  • D.I.C Addict
  • member icon

Reputation: 134
  • View blog
  • Posts: 868
  • Joined: 25-October 08

Re: Leap year validation

Posted 06 March 2010 - 10:13 AM

Well you are persistent ...

Also, I have not seen someone try so hard with so much un-needed code ...

And you don't seem to incorporate any earlier advise given ...

You did not make the assignments for the display to be seen and there were several logic errors in the code. But, because you are making the attempts, see how this would work for you.
<html>
<head>
<title>Zodiac Sign</title>
<script language="Javascript">
// For: http://www.dreamincode.net/forums/showtopic160171.htm

function dateBetween(ZM,ZD,ZY,sm,sd,em,ed) {
  flag = false;
  var testDate = new Date(ZY,ZM,ZD);
  var Bdate = new Date(ZY,sm,sd);
  var Edate = new Date(ZY,em,ed);
  if ( (testDate >= Bdate) && (testDate <= Edate) ) { flag = true; }
// alert(sm+','+sd+' : '+ZM+','+ZD+' : '+em+','+ed+' --> '+':'+flag+':');
  return flag;
}
  
function zodiac() {
//Astrological Zodiac Sign
  var ZSmonth = document.form1.month.value;
  var ZSdate  = document.form1.date.value;
  var ZSyear  = document.form1.year.value;
  
  var ZSsign  = 'blank';
  var Zcsign  = 'blank';
  if ((ZSmonth == 'x') || (ZSdate == 'y') || (ZSyear == 'z')) { alert('No selection chosen'); return }

  if (dateBetween(ZSmonth,ZSdate,ZSyear,1,20,2,18)) { ZSsign = 'Aquarius'; }
  if (dateBetween(ZSmonth,ZSdate,ZSyear,2,19,3,20)) { ZSsign = 'Pisces'; }
  if (dateBetween(ZSmonth,ZSdate,ZSyear,3,21,4,20)) { ZSsign = 'Aries'; }
  if (dateBetween(ZSmonth,ZSdate,ZSyear,4,21,5,20)) { ZSsign = 'Taurus'; }
  if (dateBetween(ZSmonth,ZSdate,ZSyear,5,21,6,20)) { ZSsign = 'Gemini'; }
  if (dateBetween(ZSmonth,ZSdate,ZSyear,6,21,7,21)) { ZSsign = 'Cancer'; }
  if (dateBetween(ZSmonth,ZSdate,ZSyear,7,22,8,21)) { ZSsign = 'Leo'; }
  if (dateBetween(ZSmonth,ZSdate,ZSyear,8,22,9,21)) { ZSsign = 'Virgo'; }
  if (dateBetween(ZSmonth,ZSdate,ZSyear,9,22,10,21)) { ZSsign = 'Libra'; }
  if (dateBetween(ZSmonth,ZSdate,ZSyear,10,22,11,21)) { ZSsign = 'Scorpio'; }
  if (dateBetween(ZSmonth,ZSdate,ZSyear,11,22,12,20)) { ZSsign = 'Sagittarius'; }

// Special case  
  if (ZSmonth == 1 && ZSdate <=19) { ZSsign = "Capricorn"; }
  if (ZSmonth == 12 && ZSdate >=21) { ZSsign = "Capricorn"; }
// alert(ZSsign);
  
//Chinse Zodiac Sign
  ZodiacSign = ['Rat','Ox','Tiger','Rabbit','Dragon','Snake',
                'Horse','Ram','Monkey','Rooster','Dog','Pig'];
  zy = (ZSyear - 1984) % 12;
  Zcsign = ZodiacSign[zy];
/* NO NEED FOR FOLLOWING WITH PRECEEDING 3 LINES
  if ((ZSyear=='1984') || (ZSyear=='1996') || (ZSyear=='2008')) { Zcsign = "Rat"; }
  if ((ZSyear=='1985') || (ZSyear=='1997') || (ZSyear=='2009')) { Zcsign = "Ox"; }
  if ((ZSyear=='1986') || (ZSyear=='1998') || (ZSyear=='2010')) { Zcsign = "Tiger"; }
  if ((ZSyear=='1987') || (ZSyear=='1999') || (ZSyear=='2011')) { Zcsign = "Rabbit"; }
  if ((ZSyear=='1988') || (ZSyear=='2000') || (ZSyear=='2012')) { Zcsign = "Dragon"; }
  if ((ZSyear=='1989') || (ZSyear=='2001') || (ZSyear=='2013')) { Zcsign = "Snake"; }
  if ((ZSyear=='1990') || (ZSyear=='2002') || (ZSyear=='2014')) { Zcsign = "Horse"; }
  if ((ZSyear=='1991') || (ZSyear=='2003') || (ZSyear=='2015')) { Zcsign = "Ram"; }
  if ((ZSyear=='1980') || (ZSyear=='1992') || (ZSyear=='2004') || (ZSyear=='2016')) { Zcsign = "Monkey"; }
  if ((ZSyear=='1981') || (ZSyear=='1993') || (ZSyear=='2005')) { Zcsign = "Rooster"; }
  if ((ZSyear=='1982') || (ZSyear=='1994') || (ZSyear=='2006')) { Zcsign = "Dog"; }
  if ((ZSyear=='1983') || (ZSyear=='1995') || (ZSyear=='2008')) { Zcsign = "Pig"; }
*/
  
/* DON'T UNDERSTAND THE PURPOSE OF THE FOLLOWING.  ALREADY HAVE DATA VALIDATION CHECKS IN 'validate()'
  if (ZSmonth == 4 && ZSdate == 31) { ZSsign = "";  Zcsign = ""; }
  if (ZSmonth == 6 && ZSdate == 31) { ZSsign = "";  Zcsign = ""; }
  if (ZSmonth == 9 && ZSdate == 31) { ZSsign = "";  Zcsign = ""; }
  if (ZSmonth == 11 && ZSdate == 31) { ZSsign = "";  Zcsign = ""; }
*/  
/* THE FOLLOWING WILL NOT WORK AT ALL BECAUSE 'selectedIndex' IS A NUMBER
  if (document.form1.month.selectedIndex == "x" 
   || document.form1.date.selectedIndex == "y" 
   || document.form1.year.selectedIndex == "z")
// put logic at the start of the function to exit before all the un-necessary checks
*/

  document.form1.sign.value = ZSsign;
  document.form1.csign.value = Zcsign;
  return
}

function validate() {
  var ZSmonth = document.form1.month.value;
  var ZSdate  = document.form1.date.value;
  var ZSyear  = document.form1.year.value;
  var ZSsign  = document.form1.sign.value;
  var validMonthLength = ['',31,28,31,30,31,30,31,31,30,31,30,31];
  
  var testDate = new Date(ZSyear,(ZSmonth-1),ZSdate);
  if ( (ZSmonth == 2) && (ZSdate != testDate.getDate()) ) { 
    alert("There is no dates above 28 in February of "+ZSyear); return false;
  }
  if (ZSmonth != 2) { 
    if (ZSdate > validMonthLength[ZSmonth]) { alert("Invalid date for month chosen"); return false; }
  }
/* un-necessary code with statements above
  if (ZSmonth == 2 && ZSdate > 29) { alert("There are only a maximum of 29 days in February."); }
  if (ZSmonth == 4 && ZSdate == 31) { alert("There are only 30 days in April."); }
  if (ZSmonth == 6 && ZSdate == 31) { alert("There are only 30 days in June."); }
  if (ZSmonth == 9 && ZSdate == 31) { alert("There are only 30 days in September."); }
  if (ZSmonth == 11 && ZSdate == 31){ alert("There are only 30 days in November."); }
  else { return true; }
*/
  zodiac();
//  return true;
  return false;		// change to true later if you really need to do something on submission
}
</script>
<body>
<form name="form1" action="" onsubmit="return validate()">
<center>
<br><br>
<table border="0" bgcolor="b0a2ff" cellspacing="0" cellpadding="1">
 <tr>
  <td>
   <font size="2" face="Trebuchet MS">
    <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Month&nbsp;</b>
   </font>
  </td>
  <td>
   <font size="2" face="Trebuchet MS">
    <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;</b>
   </font>
  </td>
  <td>
   <font size="2" face="Trebuchet MS">
    <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Year</b>&nbsp;</b>
   </font>
  </td>
  <td>
   <font size="2" face="Trebuchet MS"><b>
    &nbsp;&nbsp;&nbsp;&nbsp; Zodiac Sign
    &nbsp;&nbsp;&nbsp;&nbsp; Chinese Zodiac</b>
   </font>
  </td>
 </tr>
 <tr>
  <td>
   &nbsp;<select name="month">
    <option value="x" selected>Select&nbsp;</option>
    <option value="1">January</option>
    <option value="2">February</option>
    <option value="3">March</option>
    <option value="4">April</option>
    <option value="5">May</option>
    <option value="6">June</option>
    <option value="7">July</option>
    <option value="8">August</option>
    <option value="9">September</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">December</option>
   </select>
  </td>
  <td>
   &nbsp;<select name="date">
    <option value="y" selected>Select&nbsp;</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
    <option value="13">13</option>
    <option value="14">14</option>
    <option value="15">15</option>
    <option value="16">16</option>
    <option value="17">17</option>
    <option value="18">18</option>
    <option value="19">19</option>
    <option value="20">20</option>
    <option value="21">21</option>
    <option value="22">22</option>
    <option value="23">23</option>
    <option value="24">24</option>
    <option value="25">25</option>
    <option value="26">26</option>
    <option value="27">27</option>
    <option value="28">28</option>
    <option value="29">29</option>
    <option value="30">30</option>
    <option value="31">31</option>
   </select>
  </td>
  <td>
   &nbsp;<select name="year">
    <option value = 'z' selected>Select&nbsp;</option>
    <option value = '2016'>2016</option>
    <option value = '2015'>2015</option>
    <option value = '2014'>2014</option>
    <option value = '2013'>2013</option>
    <option value = '2012'>2012</option>
    <option value = '2011'>2011</option>
    <option value = '2010'>2010</option>
    <option value = '2009'>2009</option>
    <option value = '2008'>2008</option>
    <option value = '2007'>2007</option>
    <option value = '2006'>2006</option>
    <option value = '2005'>2005</option>
    <option value = '2004'>2004</option>
    <option value = '2003'>2003</option>
    <option value = '2002'>2002</option>
    <option value = '2001'>2001</option>
    <option value = '2000'>2000</option>
    <option value = '1999'>1999</option>
    <option value = '1998'>1998</option>
    <option value = '1997'>1997</option>
    <option value = '1996'>1996</option>
    <option value = '1995'>1995</option>
    <option value = '1994'>1994</option>
    <option value = '1993'>1993</option>
    <option value = '1992'>1992</option>
    <option value = '1991'>1991</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>
    <option value = '1980'>1980</option>
   </select>
  </td>
  <td>
   &nbsp;<input name="sign" size="12">
   &nbsp;<input name="csign" size="12">
   &nbsp;<input type="submit" value="Submit">
   &nbsp;<input type="reset" value="Reset">
  </td>
 </tr>
</table>
</center>
</form>
</body>
</html>


I left in some of your original code that can be deleted.
I left it in so that you could see what I changed with fewer lines of code.
Could be compressed further, but see what this does for your understanding.

Good Luck!
:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1