...

View Full Version : JavaScript: month-year validation



abhit_kumar
11-01-2004, 09:14 AM
i have a select type options in which we can select month and year options.

if we select future month and year, it shows error message and clear the select type options.

i have made a script as show below: buit its not working properly.



script:


function dropdown_validator()
{
// Variables for the current date, year and month
var right_now=new Date();
var the_year=right_now.getYear();
var the_month=right_now.getMonth();

// Check to see if the request is within the current year and month
if (payment_voucher.year.value == the_year && payment_voucher.month.value > the_month)
{
alert("Please check the month of your request.");
return true;
}


// Check to see if the year request is calid
if (payment_voucher.year.value > the_year)
{
alert("Please check the year of your request.");
return(false);
}
// Comment out the alert below and change the location to your destination
//alert("The request is valid\nMonth = "+ payment_voucher.month.value +"\nYear = "+ payment_voucher.year.value);
// window.location.replace= dropdown_form.u_month.value + dropdown_form.u_year.value
return(true);
}





form:


<form name="payment_voucher" ACTION="" METHOD="POST" >
<center>Current Processing Month:<select size="1" name="month" onblur="return dropdown_validator()" >
<option value="">Month</option>
<option value="1">Jan</option>
<option value="2">Feb</option>
<option value="3">Mar</option>
<option value="4">Apr</option>
<option value="5">May</option>
<option value="6">Jun</option>
<option value="7">Jul</option>
<option value="8">Aug</option>
<option value="9">Sep</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>
</select>

<select size="1" name="year" onblur="return dropdown_validator()">
<option value="">Year</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>
</select><br><br></center>


plz help me


abhijit

Brandoe85
11-01-2004, 05:58 PM
When your reading in the month the first month starts at 0, not 1. And for the year, you need to use getFullYear(); but something like this should work:


<html>
<head>
<script language="javascript">
function dropdown_validator()
{
// Variables for the current date, year and month
var right_now=new Date();
var the_year=right_now.getFullYear();
var the_month=right_now.getMonth();

// Check to see if the request is within the current year and month
if (document.payment_voucher.month.value != the_month)
{
alert("Please check the month of your request.");
return false;
}


// Check to see if the year request is calid
if (document.payment_voucher.year.value > the_year)
{
alert("Please check the year of your request.");
return false;
}
// Comment out the alert below and change the location to your destination
//alert("The request is valid\nMonth = "+ payment_voucher.month.value +"\nYear = "+ payment_voucher.year.value);
// window.location.replace= dropdown_form.u_month.value + dropdown_form.u_year.value
//return(true);
}
</script>
<form name="payment_voucher" ACTION="" METHOD="POST" >
<center>Current Processing Month:<select size="1" name="month" onblur="return dropdown_validator()" >
<option value="">Month</option>
<option value="0">Jan</option>
<option value="1">Feb</option>
<option value="2">Mar</option>
<option value="3">Apr</option>
<option value="4">May</option>
<option value="5">Jun</option>
<option value="6">Jul</option>
<option value="7">Aug</option>
<option value="8">Sep</option>
<option value="9">Oct</option>
<option value="10">Nov</option>
<option value="11">Dec</option>
</select>

<select size="1" name="year" onblur="return dropdown_validator()">
<option value="">Year</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>
</select><br><br></center>
</form>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum