4 Replies - 420 Views - Last Post: 03 September 2012 - 05:20 AM

#1 07ozo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-September 12

Create a page which allows a user to enter a credit card number and ex

Posted 03 September 2012 - 03:22 AM

<HEAD>

<script LANGUAGE="Javascript">

var Cards = new makeArray(8);
Cards[0] = new CardType;
var MasterCard = Cards[0]= new CardType();

function CheckCardNumber(form) {
var tmpyear;
if (form.CardNumber.value.length == 0) {
alert("Please enter a Card Number.");
form.CardNumber.focus();
return;
}
if (form.ExpYear.value.length == 0) {
alert("Please enter the Expiration Year.");
form.ExpYear.focus();
return;
}
if (form.ExpYear.value > 96)
tmpyear = "19" + form.ExpYear.value;
else if (form.ExpYear.value < 12)
tmpyear = "20" + form.ExpYear.value;
else {
alert("The Expiration Year is not valid.");
return;
}
tmpmonth = form.ExpMon.options[form.ExpMon.selectedIndex].value;

if (!(new CardType()).isExpiryDate(tmpyear, tmpmonth)) {
alert("This card has already expired.");
return;
}
card = form.CardType.options[form.CardType.selectedIndex].value;
var retval = eval(card + ".checkCardNumber(\"" + form.CardNumber.value +
"\", " + tmpyear + ", " + tmpmonth + ");");
cardname = "";
if (retval)

alert("This card number appears to be valid.");


else {
for (var n = 0; n < Cards.size; n++) {
if (Cards[n].checkCardNumber(form.CardNumber.value, tmpyear, tmpmonth)) {
cardname = Cards[n].getCardType();
break;
   }

else {
alert("This card number is not valid.");
      }
   }
}

function checkCardNumber() {
var argv = checkCardNumber.arguments;
var argc = checkCardNumber.arguments.length;
var cardnumber = (argc > 0) ? argv[0] : this.cardnumber;
var year = (argc > 1) ? argv[1] : this.year;
var month = (argc > 2) ? argv[2] : this.month;

this.setCardNumber(cardnumber);
this.setExpiryDate(year, month);

if (!this.isCardNumber())
return false;
if (!this.isExpiryDate())
return false;

return true;
}
function getCardType() {
return this.cardtype;
}
function getExpiryDate() {
return this.month + "/" + this.year;
}
function isCardNumber() {
var argv = isCardNumber.arguments;
var argc = isCardNumber.arguments.length;
var cardnumber = (argc > 0) ? argv[0] : this.cardnumber;
if (!this.luhnCheck())
return false;

for (var n = 0; n < this.len.size; n++)
if (cardnumber.toString().length == this.len[n]) {
for (var m = 0; m < this.rules.size; m++) {
var headdigit = cardnumber.substring(0, this.rules[m].toString().length);
if (headdigit == this.rules[m])
return true;
}
return false;
}
return false;
}

function isExpiryDate() {
var argv = isExpiryDate.arguments;
var argc = isExpiryDate.arguments.length;

year = argc > 0 ? argv[0] : this.year;
month = argc > 1 ? argv[1] : this.month;

if (!isNum(year+""))
return false;
if (!isNum(month+""))
return false;
today = new Date();
expiry = new Date(year, month);
if (today.getTime() > expiry.getTime())
return false;
else
return true;
}

function isNum(argvalue) {
argvalue = argvalue.toString();

if (argvalue.length == 0)
return false;

for (var n = 0; n < argvalue.length; n++)
if (argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9")
return false;

return true;
}

function luhnCheck() {
var argv = luhnCheck.arguments;
var argc = luhnCheck.arguments.length;

var CardNumber = argc > 0 ? argv[0] : this.cardnumber;

if (! isNum(CardNumber)) {
return false;
  }

var no_digit = CardNumber.length;
var oddoeven = no_digit & 1;
var sum = 0;

for (var count = 0; count < no_digit; count++) {
var digit = parseInt(CardNumber.charAt(count));
if (!((count & 1) ^ oddoeven)) {
digit *= 2;
if (digit > 9)
digit -= 9;
}
sum += digit;
}
if (sum % 10 == 0)
return true;
else
return false;
}

function makeArray(size) {
this.size = size;
return this;
}
function setCardNumber(cardnumber) {
this.cardnumber = cardnumber;
return this;
}
function setCardType(cardtype) {
this.cardtype = cardtype;
return this;
}
function setExpiryDate(year, month) {
this.year = year;
this.month = month;
return this;
}
function setLen(len) {
if (len.length == 0 || len == null)
len = "13,14,15,16,19";

var tmplen = len;
n = 1;
while (tmplen.indexOf(",") != -1) {
tmplen = tmplen.substring(tmplen.indexOf(",") + 1, tmplen.length);
n++;
}
this.len = new makeArray(n);
n = 0;
while (len.indexOf(",") != -1) {
var tmpstr = len.substring(0, len.indexOf(","));
this.len[n] = tmpstr;
len = len.substring(len.indexOf(",") + 1, len.length);
n++;
}
this.len[n] = len;
return this;
}
function setRules(rules) {
if (rules.length == 0 || rules == null)
rules = "0,1,2,3,4,5,6,7,8,9";
  
var tmprules = rules;
n = 1;
while (tmprules.indexOf(",") != -1) {
tmprules = tmprules.substring(tmprules.indexOf(",") + 1, tmprules.length);
n++;
}
this.rules = new makeArray(n);
n = 0;
while (rules.indexOf(",") != -1) {
var tmpstr = rules.substring(0, rules.indexOf(","));
this.rules[n] = tmpstr;
rules = rules.substring(rules.indexOf(",") + 1, rules.length);
n++;
}
this.rules[n] = rules;
return this;
}
</script>
</HEAD>
<BODY>

<form name="ThisForm">
Card Number: <input name="CardNumber" size="16" maxlength="19"><br>   
</select>
<br>
Expiration Date: Month
<select name="ExpMon">
<option value="1" selected>1
<option value="2">2
<option value="3">3
<option value="4">4
<option value="5">5
<option value="6">6
<option value="7">7
<option value="8">8
<option value="9">9
<option value="10">10
<option value="11">11
<option value="12">12
</select>
Year <input name="ExpYear" size="2" maxlength="2"><br>
<input type="button" value="Check" onclick="CheckCardNumber(this.form)"><br>
</form>

This post has been edited by RudiVisser: 03 September 2012 - 03:27 AM
Reason for edit:: Code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Create a page which allows a user to enter a credit card number and ex

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1002
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Create a page which allows a user to enter a credit card number and ex

Posted 03 September 2012 - 03:28 AM

Congratulations.

Do you have an issue with this code or are you just showing off? :)
Was This Post Helpful? 0
  • +
  • -

#3 07ozo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-September 12

Re: Create a page which allows a user to enter a credit card number and ex

Posted 03 September 2012 - 03:35 AM

View PostRudiVisser, on 03 September 2012 - 03:28 AM, said:

Congratulations.

Do you have an issue with this code or are you just showing off? :)

not like that i cant fix this problem.
Was This Post Helpful? 0
  • +
  • -

#4 Mina-no-Hime  Icon User is offline

  • D.I.C Head

Reputation: 98
  • View blog
  • Posts: 176
  • Joined: 23-August 12

Re: Create a page which allows a user to enter a credit card number and ex

Posted 03 September 2012 - 03:36 AM

@Rudi: Neither, it's a duplicate thread.

http://www.dreaminco...0&#entry1695489

This post has been edited by Mina-no-Hime: 03 September 2012 - 03:38 AM

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,479
  • Joined: 27-December 08

Re: Create a page which allows a user to enter a credit card number and ex

Posted 03 September 2012 - 05:20 AM

Please avoid duplicate posting.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1