PDA

View Full Version : javascript date problems


ted_williams
11-12-2002, 02:04 PM
I'm trying to create a javascript date validator. I've hunted up peices of it, but am having a horrible time putting it all together.
The date is coming in the format of MMYY and the variable is named card_expire.
All I really need to know is that the card_expire date is not greater than todays date (in MMYY format of course).
This is what have.
<script type="text/javascript">
function isDate(sDate) {

// split the date string
var M = sDate.substring(0,2);
var Y = sDate.substring(2);
if (M < 1 || M > 12) return false;

return (!isNaN(new Date(sDate).getTime()));
}

function final_submit(){
if( !isDate(document.signup_form.CARD_EXPIRE.value) )
{
alert( "Please enter a valid date" );
return false;
}
else return true;
}

</script>
First of all, this script always returns fail, no matter what date I enter and I don't understand if its actually comparing the card_expire to todays date.

Its kicking my butt.

My plan was to add some

//todays info
var now = new Date()
var today = new Date()
var month = today.getMonth() + 1
var year = today.getFullYear()
var newyear = year.substring(2);
var today = month + " " + year

if (year < Y) return false;
and another if for the month
but I can't get the first part to work.
Thanks.

giz
11-12-2002, 03:41 PM
Just got in from work, so no brain power left to think about that lot just right now.

I do however want to add a comment that you still need to think about making your code Year 2000 proof. If I enter an expiry date with Year 99 will your code reject it or accept it as valid?

[I can't understand why cards didn't go to a four digit year after 1999. Year-Month order would also have made processing easier. See also the ISO 8601 standard, and RFC 3339 for more date issues].

kdjoergensen
11-12-2002, 04:37 PM
function isDate(sDate) {

// split the date string
var M = sDate.substring(0,2);
var Y = sDate.substring(2);

// get existing date into similar M/Y form
var now = new Date();
var today = new Date();
var M1 = today.getMonth() + 1;
var year = today.getFullYear();
var Y1 = year.substring(2);

// now the famous if-sentence
if (Y1 > Y) { // this year (Y1) is ahead of card expiry year
callError("Card expired - check year");
return false;
} elseif ((Y1 == Y) && (M1 > M)){ // this month is ahead
callError ("Card just expired -- check month");
return false;
} else { // card is OK
return true;
}

}


function callError(msg){
alert("An error occurred -- "+msg);
return false;
}


The above if/if-else sentence is the key to success.
First if-sentence checks to see if the year of the card (Y) is less than (earlier than) the current year (Y1), and we can then quickly rule out compliance.
If we (the script) decide that the Year is ok, then we proceed and check if the month of the card is less than (earlier than) the month of the current month, and the year is the same, and if that is the case then we can also quickly rule out compliance.

e.g. we check two things:
- if the year on the card is earlier than the current year
- if the years are the same, if the month on the card is earlier than the current month.
above two would disqualify the card.
If none of above two parameters disqualify the card then it is accepted.
If not an error is thrown using a custom message.

--
follow up:
the above does not take into account the problem highlighted about 4-digit years. e.g. if 03 > 99 for example. You should probably put something like this in:

if (Y>50){ // e.g. 1951-1999
Y = 1900+Y;
} else { // e.g. 2000-2049
Y = 2000+Y;
}
and then apply same logic to the Y1 calculation.

That script makes the assumption that any number typed in by the user (Y) which is greater than 50 would apply to 1900's e.g. 1951-1999 whereas any number less than 50 would apply to 2000's e.g. 2000-2050. This is obviously entirely an assumption (but probably an ok one for now).

ted_williams
11-12-2002, 05:43 PM
how do I call that script?
with a onsubmit="return isDate();" in the <form> tag?
I'm trying that and I'm erroring out with a "undefined" is null or not an object
um... I can't find the word "undefined" on any of the pages the browser should be looking at.

also, tiny thing for the next guy who trys to use it
// now the famous if-sentence
if (Y1 > Y) {
// this year (Y1) is ahead of card expiry year
callError("Card expired - check year");
return false;
} else if ((Y1 == Y) && (M1 > M)){
// this month is ahead
callError ("Card just expired -- check month");
return false;
} else {
// card is OK
return true;
}
}
else if with a space
I want to save anyone else the time it took me to hunt that down
I program in cold fusion, so it looked ok to me for a long time

giz
11-12-2002, 06:15 PM
Your If statement only works for a four-digit year.
[A cautionary note to those who read this when it is archived].