tags:

views:

105

answers:

5

I have developed a script for checking that user has selected a valid month and year for credit card.

function validatemonth()
{
var dt = new Date();
var mth = dt.getMonth();
var yr = dt.getYear(); //this seems to return different data in different browsers
yr = yr + 1900;

if(eval(document.PurchaseCredit.cc_expire_month.value) < mth && eval(document.PurchaseCredit.cc_expire_year.value) == yr)
{
 document.getElementById('error').innerHTML = "Expiry Date cannot be less than current date."; 
 document.forms['PurchaseCredit'].submit.disabled = true;
}
else
{
 document.getElementById('error').innerHTML = ""; 
 document.forms['PurchaseCredit'].submit.disabled = false;
}
}

This script works well in FireFox but does not work in IE7. Why?

+3  A: 

Try:

var yr = dt.getFullYear();
GoodEnough
+5  A: 

Date.getYear() is browser dependent. In Firefox it returns the number of years after 1900, in IE it returns the full 4 digit year.

Date.getFullYear() returns the 4 digit year in all browsers, so you should always use it instead.

Gerald
+3  A: 

Try using getFullYear() instead of getYear() - it should work in both. Also, if you use getFullYear(), remove the "yr = yr + 1900" line as well

Amit G
+3  A: 

getYear is deprecated. Use getFullYear instead.

Gumbo
+8  A: 

Its not working because getYear() return different results for different browsers.

Internet Explorer:

* Returns four digits for years before 1900 and after 1999.

Firefox:

* Returns a value less than 0 for years before 1900. For example, the

year 1800 returns -100. * Returns a value 100 or greater for years greater than or equal to 2000. For example, the year 2008 returns 108.

Your addition of 1900 is relevant only for Firefox.

Easy Solution : Try using getFullYear()

Aditya Sehgal