Hi All
Wonder if you can help me with the following calc, which I want to create to
check whether or not a user has entered a valid credit card expiry date:
1) I have retrieve a value from 2 x SELECTs. One value is the month in a 2
DIGIT form, eg 01, 02, 03, 04, etc, and the other is the year in a 4 DIGIT
form, ie 2004, 2005, 2006, 2007, etc.
2) My Maths for this was going to be that I do a calc on the figures
retrieved in the above point1 against figures retrieved for today's date, in
theory like the following:
expirydate = expirymonth + (expiryyear * 12) // this is for the expiry date
figures in point 1
currentdate = currentmonth + (currentyear * 12) // this is for today's date
figures.
If expirydate < currentdate then bring up a warnnig box that they can bypass
else drop out.
Because the expirydate and currentdate vars are standard ints this is a
straightforward calc, but my probs in getting this to work are as follows:
a) Because all of my months have 2 digits, ie 01, 02, .... 10, 11, 12, there
seems to be a problem calculating the ones that start with a zero, eg 01,
02, etc, but I need to keep to this 2 digit display.
b) My year is 4 digits, which I need to keep, so I need to chop it to 2.
c) I used the following to get today's month and year:
thedate = Date()
currentmonth = thedate.CurrentMonth()
currentyear = thedate.CurrentYear()
which seems to work, but then I can't seem to strip these down to the 1
digit month and 2 digit year that I am after AND use them in a straight
integer calc, eg currentdate = currentmonth + (currentyear * 12).
d) If the expirydate is less than the currentdate I wanted to display a
OK/Cancel box so that I can warn them of this, but if they click OK the
submit is true and goes through and if they click Cancel then the form is
false and the submit doesn't go through.
Can anybody advise me on the above?
Thanks
Robbie
RobG - 29 Nov 2004 20:34 GMT
> Hi All
>
[quoted text clipped - 8 lines]
> retrieved in the above point1 against figures retrieved for today's date, in
> theory like the following:
[...]
If you create ISO8601 dates of the form yyyymm, you can compare them
dates using string concatenation of your variables.
If you need help with that, let us know.
Do not trust the date is set to on the client, check everything on the
server.

Signature
Rob.
Fred Oz - 29 Nov 2004 23:22 GMT
>> Hi All
>>
>> Wonder if you can help me with the following calc, which I want to
>> create to check whether or not a user has entered a valid credit card
>> expiry date:
[...]
> If you create ISO8601 dates of the form yyyymm, you can compare them
> dates using string concatenation of your variables.
[quoted text clipped - 3 lines]
> Do not trust the date is set to on the client, check everything on the
> server.
Yes. One way is to send a check date in the page sent to the client -
a meta tag will do the trick, but there are other methods. The check
date is then based on the server and not whatever the client may have
set.
Below is some sample code, note that I haven't done any validation of
the input since it is supposed to come from a select list that should
ensure it is OK. As always, check everything again on the server.
<html><head>
<title>untitled</title>
<meta name="checkdate" content="2004-11">
<script type="text/javascript">
function checkDate(y,m){
var expDate = y + m;
if(document.getElementsByTagName) {
var metaTags = document.getElementsByTagName('meta');
for (var i=0; i<metaTags.length; i++) {
if (metaTags[i].name == 'checkdate') {
var checkYear = metaTags[i].content.split('-')[0];
var checkMonth = metaTags[i].content.split('-')[1];
break;
}
}
var checkDate = checkYear + checkMonth;
if (expDate >= checkDate) {
alert('Card expiry date ' + y
+ '-' + m + ' is OK')
} else {
alert('Sorry, Card expiry date ' + y
+ '-' + m + ' has expired')
}
}
}
</script>
</head>
<body>
<form action="">
<input type="text" name="year" value="2005">Enter a year (yyyy)<br>
<input type="text" name="month" value="10">Enter a month (mm)<br>
<input type="button" value="Check date" onclick="
checkDate(this.form.year.value,this.form.month.value);
this.blur();
">
<input type="reset">
</body></html>

Signature
Fred
Dr John Stockton - 30 Nov 2004 14:22 GMT
JRS: In article <41ab88d1$0$25766$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Tue, 30 Nov 2004 06:34:29, seen in
news:comp.lang.javascript, RobG <rgqld@iinet.net.auau> posted :
>> Wonder if you can help me with the following calc, which I want to create to
>> check whether or not a user has entered a valid credit card expiry date:
[quoted text clipped - 15 lines]
> Do not trust the date is set to on the client, check everything on the
> server.
To get the client date as yyyymm, one can use
with (new Date()) S = String(getFullYear()*100+getMonth()+1)
or, short-term,
with (new Date()) S = String(getYear()%100*100+getMonth()+200001)
IMHO, whether or not to trust the client date depends on how much the
author cares whether the user screws up.
Given that a user can subvert a reply, anything that matters to the
author must be checked at the author's end.
There is a problem in validating credit card dates, which particularly
affects New Zealanders visiting Hawaii on the last date of validity, and
/vice versa/.
The OP's javascript engine seems to have Date methods not authorised by
ECMA-262 Edn 3.

Signature
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.