Solved

URGENT ::: I need help making my credit card vaildator to work... its javascript --500points

Posted on 2004-10-06
12
362 Views
Last Modified: 2012-06-21
Ok, I need this script customized for my feildname's and to find out how to make it execute on button click.


Feild names :
cc_type = Card Type Selection
cc_number = Card Number
expdate_month = Card expiration Month
expdate_year = Card expiration Year
cc_vnumber = Card vercation number


====================================
====================================


<SCRIPT LANGUAGE="JavaScript">
<!-- Begin

var Cards = new makeArray(8);
Cards[0] = new cc_type("MasterCard", "51,52,53,54,55", "16");
var MasterCard = Cards[0];
Cards[1] = new cc_type("VisaCard", "4", "13,16");
var VisaCard = Cards[1];
Cards[2] = new cc_type("AmExCard", "34,37", "15");
var AmExCard = Cards[2];
Cards[3] = new cc_type("DinersClubCard", "30,36,38", "14");
var DinersClubCard = Cards[3];
Cards[4] = new cc_type("DiscoverCard", "6011", "16");
var DiscoverCard = Cards[4];
Cards[5] = new cc_type("enRouteCard", "2014,2149", "15");
var enRouteCard = Cards[5];
Cards[6] = new cc_type("JCBCard", "3088,3096,3112,3158,3337,3528", "16");
var JCBCard = Cards[6];
var LuhnCheckSum = Cards[7] = new cc_type();

function CheckCreditCard(Index,cc_number,expdate_month,expdate_year) {
var today = new Date()
var thisyear = today.getYear()
var thismonth = today.getMonth()+1

if ((eval(expdate_year) < thisyear)||((eval(expdate_year)==thisyear)&&(eval(expdate_month)<thismonth))) {
alert("This card has already expired.");
orders.elements[Index+2].focus();
return false;
}


if (cc_number.length<13) {
alert("Please enter a valid credit card number.");
orders.elements[Index].focus();
return false;
}


if (!luhnCheck(cc_number)) {
alert("Please enter a valid credit card number.");
orders.elements[Index].focus();
return false;
}

}
/*************************************************************************\
Object cc_type([String cc_type, String rules, String len, int year,
                                        int month])
cc_type    : type of card, eg: MasterCard, Visa, etc.
rules       : rules of the cc_number, eg: "4", "6011", "34,37".
len         : valid length of cc_number, eg: "16,19", "13,16".
year        : year of Expiration date.
month       : month of Expiration date.
eg:
var VisaCard = new cc_type("Visa", "4", "16");
var AmExCard = new cc_type("AmEx", "34,37", "15");
\*************************************************************************/
function cc_type() {
var n;
var argv = cc_type.arguments;
var argc = cc_type.arguments.length;

this.objname = "object cc_type";

var tmpcc_type = (argc > 0) ? argv[0] : "CardObject";
var tmprules = (argc > 1) ? argv[1] : "0,1,2,3,4,5,6,7,8,9";
var tmplen = (argc > 2) ? argv[2] : "13,14,15,16,19";

this.setcc_number = setcc_number;  // set cc_number method.
this.setcc_type = setcc_type;  // setcc_type method.
this.setLen = setLen;  // setLen method.
this.setRules = setRules;  // setRules method.
this.setExpirationDate = setExpirationDate;  // setExpirationDate method.

this.setcc_type(tmpcc_type);
this.setLen(tmplen);
this.setRules(tmprules);
if (argc > 4)
this.setExpirationDate(argv[3], argv[4]);

this.checkcc_number = checkcc_number;  // checkcc_number method.
this.getExpirationDate = getExpirationDate;  // getExpirationDate method.
this.getcc_type = getcc_type;  // getcc_type method.
this.iscc_number = iscc_number;  // iscc_number method.
this.isExpirationDateValid = isExpirationDateValid;  // isExpirationDateValid method.
this.luhnCheck = luhnCheck;// luhnCheck method.
return this;
}

/*************************************************************************\
boolean checkcc_number([String cc_number, int year, int month])
return true if cc_number pass the luhncheck and the Expiration date is
valid, else return false.
\*************************************************************************/
function checkcc_number() {
var argv = checkcc_number.arguments;
var argc = checkcc_number.arguments.length;
var cc_number = (argc > 0) ? argv[0] : this.cc_number;
var year = (argc > 1) ? argv[1] : this.year;
var month = (argc > 2) ? argv[2] : this.month;

this.setcc_number(cc_number);
this.setExpirationDate(year, month);

if (!this.iscc_number())
return false;
if (!this.isExpirationDateValid())
return false;

return true;
}
/*************************************************************************\
String getcc_type()
return the cc_type.
\*************************************************************************/
function getcc_type() {
return this.cc_type;
}
/*************************************************************************\
String getExpirationDate()
return the Expiration date.
\*************************************************************************/
function getExpirationDate() {
return this.month + "/" + this.year;
}
/*************************************************************************\
boolean iscc_number([String cc_number])
return true if cc_number pass the luhncheck and the rules, else return
false.
\*************************************************************************/
function iscc_number() {
var argv = iscc_number.arguments;
var argc = iscc_number.arguments.length;
var cc_number = (argc > 0) ? argv[0] : this.cc_number;
if (!this.luhnCheck())
return false;

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

/*************************************************************************\
boolean isExpirationDateValid([int year, int month])
return true if the date is a valid Expiration date,
else return false.
\*************************************************************************/
function isExpirationDateValid() {
var argv = isExpirationDateValid.arguments;
var argc = isExpirationDateValid.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();
Expiration = new Date(year, month);
alert(Expiration);
if (today.getTime() > Expiration.getTime())
return false;
else
return true;
}

/*************************************************************************\
boolean isNum(String argvalue)
return true if argvalue contains only numeric characters,
else return false.
\*************************************************************************/
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;
}

/*************************************************************************\
boolean luhnCheck([String cc_number])
return true if cc_number pass the luhn check else return false.
Reference: http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl
\*************************************************************************/
function luhnCheck() {
var argv = luhnCheck.arguments;
var argc = luhnCheck.arguments.length;

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

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

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

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

/*************************************************************************\
ArrayObject makeArray(int size)
return the array object in the size specified.
\*************************************************************************/
function makeArray(size) {
this.size = size;
return this;
}

/*************************************************************************\
cc_type setcc_number(cc_number)
return the cc_type object.
\*************************************************************************/
function setcc_number(cc_number) {
this.cc_number = cc_number;
return this;
}

/*************************************************************************\
cc_type setcc_type(cc_type)
return the cc_type object.
\*************************************************************************/
function setcc_type(cc_type) {
this.cc_type = cc_type;
return this;
}

/*************************************************************************\
cc_type setExpirationDate(expdate_year, expdate_month)
return the cc_type object.
\*************************************************************************/
function setExpirationDate(expdate_year, expdate_month) {
this.year = expdate_year;
this.month = expdate_month;
return this;
}

/*************************************************************************\
cc_type setLen(len)
return the cc_type object.
\*************************************************************************/
function setLen(len) {
// Create the len array.
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;
}

/*************************************************************************\
cc_type setRules()
return the cc_type object.
\*************************************************************************/
function setRules(rules) {
// Create the rules array.
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;
}

//  End -->
</script>
0
Comment
Question by:Caiapfas
  • 7
  • 5
12 Comments
 
LVL 2

Expert Comment

by:Adesso21
ID: 12240922
Dude .. MONSTER of a script.. but I made it work as you wanted.. the only problem is that I don't have a Credit Card number to test it with...

There is lots of code syntax errors that I fixed .. but all seems fine.. as for the logic.. I can't really say.. but Have fun ..

xxxxxxxxxxxxxxxxxxxxxxxxxxxxx Code starts xxxxxxxxxxxxxxxxxxxxxxxxxxxx
<!DOCTYPE html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Credit Card Validator</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var Cards = new makeArray(8);
      Cards[0] = new cc_type("MasterCard", "51,52,53,54,55", "16");
var MasterCard = Cards[0];
      Cards[1] = new cc_type("VisaCard", "4", "13,16");
var VisaCard = Cards[1];
      Cards[2] = new cc_type("AmExCard", "34,37", "15");
var AmExCard = Cards[2];
      Cards[3] = new cc_type("DinersClubCard", "30,36,38", "14");
var DinersClubCard = Cards[3];
      Cards[4] = new cc_type("DiscoverCard", "6011", "16");
var DiscoverCard = Cards[4];
      Cards[5] = new cc_type("enRouteCard", "2014,2149", "15");
var enRouteCard = Cards[5];
      Cards[6] = new cc_type("JCBCard", "3088,3096,3112,3158,3337,3528", "16");
var JCBCard = Cards[6];
var LuhnCheckSum = Cards[7] = new cc_type();

function CheckCreditCard(Index,cc_number,expdate_month,expdate_year) {
      var today = new Date()
      var thisyear = today.getYear()
      var thismonth = today.getMonth()+1
      if ((eval(expdate_year) < thisyear)||((eval(expdate_year)==thisyear)&&(eval(expdate_month)<thismonth))) {
            alert("This card has already expired.");
            //orders.elements[Index+2].focus();
            return false;
      }
      if (cc_number.length<13) {
            alert("Please enter a valid credit card number.");
            //orders.elements[Index].focus();
            return false;
      }
      if (!luhnCheck(cc_number)) {
            alert("Please enter a valid credit card number.");
            //orders.elements[Index].focus();
            return false;
      }
}
/*************************************************************************\
Object cc_type([String cc_type, String rules, String len, int year,
                                        int month])
cc_type    : type of card, eg: MasterCard, Visa, etc.
rules       : rules of the cc_number, eg: "4", "6011", "34,37".
len         : valid length of cc_number, eg: "16,19", "13,16".
year        : year of Expiration date.
month       : month of Expiration date.
eg:
var VisaCard = new cc_type("Visa", "4", "16");
var AmExCard = new cc_type("AmEx", "34,37", "15");
\*************************************************************************/
function cc_type() {
      var n;
      var argv = cc_type.arguments;
      var argc = cc_type.arguments.length;
      this.objname = "object cc_type";

      var tmpcc_type = (argc > 0) ? argv[0] : "CardObject";
      var tmprules = (argc > 1) ? argv[1] : "0,1,2,3,4,5,6,7,8,9";
      var tmplen = (argc > 2) ? argv[2] : "13,14,15,16,19";

      this.setcc_number = setcc_number;  // set cc_number method.
      this.setcc_type = setcc_type;  // setcc_type method.
      this.setLen = setLen;  // setLen method.
      this.setRules = setRules;  // setRules method.
      this.setExpirationDate = setExpirationDate;  // setExpirationDate method.

      this.setcc_type(tmpcc_type);
      this.setLen(tmplen);
      this.setRules(tmprules);
      if (argc > 4) {
            this.setExpirationDate(argv[3], argv[4]);
      }
      this.checkcc_number = checkcc_number;  // checkcc_number method.
      this.getExpirationDate = getExpirationDate;  // getExpirationDate method.
      this.getcc_type = getcc_type;  // getcc_type method.
      this.iscc_number = iscc_number;  // iscc_number method.
      this.isExpirationDateValid = isExpirationDateValid;  // isExpirationDateValid method.
      this.luhnCheck = luhnCheck;// luhnCheck method.
      return this;
}

/*************************************************************************\
boolean checkcc_number([String cc_number, int year, int month])
return true if cc_number pass the luhncheck and the Expiration date is
valid, else return false.
\*************************************************************************/
function checkcc_number() {
      var argv = checkcc_number.arguments;
      var argc = checkcc_number.arguments.length;
      var cc_number = (argc > 0) ? argv[0] : this.cc_number;
      var year = (argc > 1) ? argv[1] : this.year;
      var month = (argc > 2) ? argv[2] : this.month;
      
      this.setcc_number(cc_number);
      this.setExpirationDate(year, month);

      if (!this.iscc_number()) {
            return false;
      }
      if (!this.isExpirationDateValid()) {
            return false;
      }
      return true;
}
/*************************************************************************\
String getcc_type()
return the cc_type.
\*************************************************************************/
function getcc_type() {
      return this.cc_type;
}
/*************************************************************************\
String getExpirationDate()
return the Expiration date.
\*************************************************************************/
function getExpirationDate() {
      return this.month + "/" + this.year;
}
/*************************************************************************\
boolean iscc_number([String cc_number])
return true if cc_number pass the luhncheck and the rules, else return
false.
\*************************************************************************/
function iscc_number() {
      var argv = iscc_number.arguments;
      var argc = iscc_number.arguments.length;
      var cc_number = (argc > 0) ? argv[0] : this.cc_number;
      if (!this.luhnCheck()) {
            return false;
      }

      for (var n = 0; n < this.len.size; n++) {
            if (cc_number.toString().length == this.len[n]) {
                  for (var m = 0; m < this.rules.size; m++) {
                        var headdigit = cc_number.substring(0, this.rules[m].toString().length);
                        if (headdigit == this.rules[m]) {
                              return true;
                        }
                  }
                  return false;
            }
      return false;
      }
}
/*************************************************************************\
boolean isExpirationDateValid([int year, int month])
return true if the date is a valid Expiration date,
else return false.
\*************************************************************************/
function isExpirationDateValid() {
      var argv = isExpirationDateValid.arguments;
      var argc = isExpirationDateValid.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();
      Expiration = new Date(year, month);
      alert(Expiration);
      if (today.getTime() > Expiration.getTime()) {
            return false;
      }else {
            return true;
      }
}
/*************************************************************************\
boolean isNum(String argvalue)
return true if argvalue contains only numeric characters,
else return false.
\*************************************************************************/
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;
      }
}
/*************************************************************************\
boolean luhnCheck([String cc_number])
return true if cc_number pass the luhn check else return false.
Reference: http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl
\*************************************************************************/
function luhnCheck() {
      var argv = luhnCheck.arguments;
      var argc = luhnCheck.arguments.length;
      var cc_number = argc > 0 ? argv[0] : this.cc_number;

      if (! isNum(cc_number)) {
            return false;
      }
      var no_digit = cc_number.length;
      var oddoeven = no_digit & 1;
      var sum = 0;
      for (var count = 0; count < no_digit; count++) {
            var digit = parseInt(cc_number.charAt(count));
            if (!((count & 1) ^ oddoeven)) {
                  digit *= 2;
                  if (digit > 9) {
                        digit -= 9;
                  }
                  sum += digit;
            }
            if (sum % 10 == 0) {
                  return true;
            }else {
                  return false;
            }
      }
}
/*************************************************************************\
ArrayObject makeArray(int size)
return the array object in the size specified.
\*************************************************************************/
function makeArray(size) {
      this.size = size;
      return this;
}

/*************************************************************************\
cc_type setcc_number(cc_number)
return the cc_type object.
\*************************************************************************/
function setcc_number(cc_number) {
      this.cc_number = cc_number;
      return this;
}

/*************************************************************************\
cc_type setcc_type(cc_type)
return the cc_type object.
\*************************************************************************/
function setcc_type(cc_type) {
      this.cc_type = cc_type;
      return this;
}

/*************************************************************************\
cc_type setExpirationDate(expdate_year, expdate_month)
return the cc_type object.
\*************************************************************************/
function setExpirationDate(expdate_year, expdate_month) {
      this.year = expdate_year;
      this.month = expdate_month;
      return this;
}

/*************************************************************************\
cc_type setLen(len)
return the cc_type object.
\*************************************************************************/
function setLen(len) {
      // Create the len array.
      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;
}

/*************************************************************************\
cc_type setRules()
return the cc_type object.
\*************************************************************************/
function setRules(rules) {
      // Create the rules array.
      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;
}

function Validate(FObj) {
      //Index is a reffrence to the actual page elements
      var IsValid = CheckCreditCard("",FObj.ccnum,FObj.expmonth,FObj.expyear);
      alert("Your Credit Card validates as "+IsValid);
      return false; // Just to stop the page from actually POSTING change to tru if you are happy.
}
//  End -->
</SCRIPT>
</HEAD>
<BODY>
Validating the Credit Card<br>
Please enter your Credit Card Details below
<form onSubmit="return Validate(this)" action="" method="POST">
<table>
<tr>
      <td>Credit Card number</td>
      <td><input type="text" name="ccnum"></td>
</tr>
<tr>
      <td>Expire Month</td>
      <td><input type="text" name="expmonth"></td>
</tr>
<tr>
      <td>Expire Year</td>
      <td><input type="text" name="expyear"></td>
</tr>
<tr>
      <td colspan="2"><input type="Submit" value="Validate"></td>
</tr>
</table>
</form>
</BODY>
</HTML>
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx Code ends xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
0
 
LVL 2

Author Comment

by:Caiapfas
ID: 12240982
it doesnt work...


I think mainly due to the fact theres no drop down for selecting a card type , which sets the math formual..please help
0
 
LVL 2

Expert Comment

by:Adesso21
ID: 12241052
OOops .. small change to the Validate function...

                var IsValid = CheckCreditCard("",FObj.ccnum.value,FObj.expmonth.value,FObj.expyear.value);

this is just to pass the value of the text field and not the actual text field Object.. to test that the right values are being passed to the main function add the following line in the CheckCreditCard function before the first IF

      alert(cc_number+"||"+expdate_month+"/"+expdate_year);

Good Luck
0
 
LVL 2

Author Comment

by:Caiapfas
ID: 12241130
wait..i'm lost...i hate to ask..but can you put it all together.. i get errors..
and still no place to select card type...tying to add .errors

thanks
0
 
LVL 2

Expert Comment

by:Adesso21
ID: 12241662
<!DOCTYPE html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Credit Card Validator</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var Cards = new makeArray(8);
      Cards[0] = new cc_type("MasterCard", "51,52,53,54,55", "16");
var MasterCard = Cards[0];
      Cards[1] = new cc_type("VisaCard", "4", "13,16");
var VisaCard = Cards[1];
      Cards[2] = new cc_type("AmExCard", "34,37", "15");
var AmExCard = Cards[2];
      Cards[3] = new cc_type("DinersClubCard", "30,36,38", "14");
var DinersClubCard = Cards[3];
      Cards[4] = new cc_type("DiscoverCard", "6011", "16");
var DiscoverCard = Cards[4];
      Cards[5] = new cc_type("enRouteCard", "2014,2149", "15");
var enRouteCard = Cards[5];
      Cards[6] = new cc_type("JCBCard", "3088,3096,3112,3158,3337,3528", "16");
var JCBCard = Cards[6];
var LuhnCheckSum = Cards[7] = new cc_type();

function CheckCreditCard(Index,cc_number,expdate_month,expdate_year) {
      var today = new Date();
      var thisyear = today.getYear();
      var thismonth = today.getMonth()+1;
      if ((eval(expdate_year) < thisyear)||((eval(expdate_year)==thisyear)&&(eval(expdate_month)<thismonth))) {
            alert("This card has already expired.");
            //orders.elements[Index+2].focus();
            return false;
      }
      if (cc_number.length<13) {
            alert("Please enter a valid credit card number.");
            //orders.elements[Index].focus();
            return false;
      }
      if (!luhnCheck(cc_number)) {
            alert("Please enter a valid credit card number.");
            //orders.elements[Index].focus();
            return false;
      }
      return true
}
/*************************************************************************\
Object cc_type([String cc_type, String rules, String len, int year,
                                        int month])
cc_type    : type of card, eg: MasterCard, Visa, etc.
rules       : rules of the cc_number, eg: "4", "6011", "34,37".
len         : valid length of cc_number, eg: "16,19", "13,16".
year        : year of Expiration date.
month       : month of Expiration date.
eg:
var VisaCard = new cc_type("Visa", "4", "16");
var AmExCard = new cc_type("AmEx", "34,37", "15");
\*************************************************************************/
function cc_type() {
      var n;
      var argv = cc_type.arguments;
      var argc = cc_type.arguments.length;
      this.objname = "object cc_type";

      var tmpcc_type = (argc > 0) ? argv[0] : "CardObject";
      var tmprules = (argc > 1) ? argv[1] : "0,1,2,3,4,5,6,7,8,9";
      var tmplen = (argc > 2) ? argv[2] : "13,14,15,16,19";

      this.setcc_number = setcc_number;  // set cc_number method.
      this.setcc_type = setcc_type;  // setcc_type method.
      this.setLen = setLen;  // setLen method.
      this.setRules = setRules;  // setRules method.
      this.setExpirationDate = setExpirationDate;  // setExpirationDate method.

      this.setcc_type(tmpcc_type);
      this.setLen(tmplen);
      this.setRules(tmprules);
      if (argc > 4) {
            this.setExpirationDate(argv[3], argv[4]);
      }
      this.checkcc_number = checkcc_number;  // checkcc_number method.
      this.getExpirationDate = getExpirationDate;  // getExpirationDate method.
      this.getcc_type = getcc_type;  // getcc_type method.
      this.iscc_number = iscc_number;  // iscc_number method.
      this.isExpirationDateValid = isExpirationDateValid;  // isExpirationDateValid method.
      this.luhnCheck = luhnCheck;// luhnCheck method.
      return this;
}

/*************************************************************************\
boolean checkcc_number([String cc_number, int year, int month])
return true if cc_number pass the luhncheck and the Expiration date is
valid, else return false.
\*************************************************************************/
function checkcc_number() {
      var argv = checkcc_number.arguments;
      var argc = checkcc_number.arguments.length;
      var cc_number = (argc > 0) ? argv[0] : this.cc_number;
      var year = (argc > 1) ? argv[1] : this.year;
      var month = (argc > 2) ? argv[2] : this.month;
      
      this.setcc_number(cc_number);
      this.setExpirationDate(year, month);

      if (!this.iscc_number()) {
            return false;
      }
      if (!this.isExpirationDateValid()) {
            return false;
      }
      return true;
}
/*************************************************************************\
String getcc_type()
return the cc_type.
\*************************************************************************/
function getcc_type() {
      return this.cc_type;
}
/*************************************************************************\
String getExpirationDate()
return the Expiration date.
\*************************************************************************/
function getExpirationDate() {
      return this.month + "/" + this.year;
}
/*************************************************************************\
boolean iscc_number([String cc_number])
return true if cc_number pass the luhncheck and the rules, else return
false.
\*************************************************************************/
function iscc_number() {
      var argv = iscc_number.arguments;
      var argc = iscc_number.arguments.length;
      var cc_number = (argc > 0) ? argv[0] : this.cc_number;
      if (!this.luhnCheck()) {
            return false;
      }

      for (var n = 0; n < this.len.size; n++) {
            if (cc_number.toString().length == this.len[n]) {
                  for (var m = 0; m < this.rules.size; m++) {
                        var headdigit = cc_number.substring(0, this.rules[m].toString().length);
                        if (headdigit == this.rules[m]) {
                              return true;
                        }
                  }
                  return false;
            }
      return false;
      }
}
/*************************************************************************\
boolean isExpirationDateValid([int year, int month])
return true if the date is a valid Expiration date,
else return false.
\*************************************************************************/
function isExpirationDateValid() {
      var argv = isExpirationDateValid.arguments;
      var argc = isExpirationDateValid.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();
      Expiration = new Date(year, month);
      alert(Expiration);
      if (today.getTime() > Expiration.getTime()) {
            return false;
      }else {
            return true;
      }
}
/*************************************************************************\
boolean isNum(String argvalue)
return true if argvalue contains only numeric characters,
else return false.
\*************************************************************************/
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;
      }
}
/*************************************************************************\
boolean luhnCheck([String cc_number])
return true if cc_number pass the luhn check else return false.
Reference: http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl
\*************************************************************************/
function luhnCheck() {
      var argv = luhnCheck.arguments;
      var argc = luhnCheck.arguments.length;
      var cc_number = argc > 0 ? argv[0] : this.cc_number;

      if (! isNum(cc_number)) {
            return false;
      }
      var no_digit = cc_number.length;
      var oddoeven = no_digit & 1;
      var sum = 0;
      for (var count = 0; count < no_digit; count++) {
            var digit = parseInt(cc_number.charAt(count));
            if (!((count & 1) ^ oddoeven)) {
                  digit *= 2;
                  if (digit > 9) {
                        digit -= 9;
                  }
                  sum += digit;
            }
            if (sum % 10 == 0) {
                  return true;
            }else {
                  return false;
            }
      }
}
/*************************************************************************\
ArrayObject makeArray(int size)
return the array object in the size specified.
\*************************************************************************/
function makeArray(size) {
      this.size = size;
      return this;
}

/*************************************************************************\
cc_type setcc_number(cc_number)
return the cc_type object.
\*************************************************************************/
function setcc_number(cc_number) {
      this.cc_number = cc_number;
      return this;
}

/*************************************************************************\
cc_type setcc_type(cc_type)
return the cc_type object.
\*************************************************************************/
function setcc_type(cc_type) {
      this.cc_type = cc_type;
      return this;
}

/*************************************************************************\
cc_type setExpirationDate(expdate_year, expdate_month)
return the cc_type object.
\*************************************************************************/
function setExpirationDate(expdate_year, expdate_month) {
      this.year = expdate_year;
      this.month = expdate_month;
      return this;
}

/*************************************************************************\
cc_type setLen(len)
return the cc_type object.
\*************************************************************************/
function setLen(len) {
      // Create the len array.
      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;
}

/*************************************************************************\
cc_type setRules()
return the cc_type object.
\*************************************************************************/
function setRules(rules) {
      // Create the rules array.
      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;
}

function Validate(FObj) {
      var CType = FObj.cctype[FObj.cctype.selectedIndex].value;
      Cards[CType].setcc_number(FObj.ccnum.value);
      Cards[CType].setExpirationDate(FObj.expyear.value,FObj.expmonth.value);
      var msg = ""
      msg += "\n" + Cards[CType].getcc_type();
      msg += "\n" + Cards[CType].cc_number;
      msg += "\n" + Cards[CType].getExpirationDate();
      msg += "\nYour card validates as " + Cards[CType].checkcc_number();
      alert(msg);
      return false; // Just to stop the page from actually POSTING change to tru if you are happy.
}
//  End -->
</SCRIPT>
</HEAD>
<BODY>
Validating the Credit Card<br>
Please enter your Credit Card Details below
<form onSubmit="return Validate(this)" action="" method="POST">
<table>
<tr>
      <td>Credit Card type</td>
      <td>
      <select name="cctype">
            <option value="0">MasterCard</option>
            <option value="1">VisaCard</option>
            <option value="2">AmExCard</option>
            <option value="3">DinersClubCard</option>
            <option value="4">DiscoverCard</option>
            <option value="5">enRouteCard</option>
            <option value="6">JCBCard</option>
      </select>
      </td>
</tr>
<tr>
      <td>Credit Card number</td>
      <td><input type="text" name="ccnum"></td>
</tr>
<tr>
      <td>Expire Month {MM}</td>
      <td><input type="text" name="expmonth"></td>
</tr>
<tr>
      <td>Expire Year {CCYY}</td>
      <td><input type="text" name="expyear"></td>
</tr>
<tr>
      <td colspan="2"><input type="Submit" value="Validate"></td>
</tr>
</table>
</form>
</BODY>
</HTML>
0
 
LVL 2

Expert Comment

by:Adesso21
ID: 12241688
That is the best I can do for you Dude ... You are going to have to study some of this code.. and try to make sense out of it ..
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 2

Author Comment

by:Caiapfas
ID: 12340825
still not working, returns all as flase..and yes i'm using really card..lol
0
 
LVL 2

Expert Comment

by:Adesso21
ID: 12341098
That is great.. I am glad to see progress..

Then next thing you'll need is working knowlage of the card algoritms. This I can unfortunatly not help you with as I know only Master Card and Visa as commencial brand in my own Country. That you' have to source at your own discretion. Once

The Object oriantated programming seems rather well though out and planned, but there is code missing, and I am assuming by now that you are not the one to have written it, be that as it may you can very well learn from it. If you have any other script related questions to add, I'll be happy to assit you..

Good Luck
Adesso21
0
 
LVL 2

Author Comment

by:Caiapfas
ID: 12405223
ok it works perfectly now...
but i need it to STOP submiting of form on error...can you help...


=-===========================================

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
var Cards = new makeArray(8);
Cards[0] = new cc_type("MasterCard", "51,52,53,54,55", "16");
var MasterCard = Cards[0];
Cards[1] = new cc_type("VisaCard", "4", "13,16");
var VisaCard = Cards[1];
Cards[2] = new cc_type("AmExCard", "34,37", "15");
var AmExCard = Cards[2];
Cards[3] = new cc_type("DinersClubCard", "30,36,38", "14");
var DinersClubCard = Cards[3];
Cards[4] = new cc_type("DiscoverCard", "6011", "16");
var DiscoverCard = Cards[4];
Cards[5] = new cc_type("enRouteCard", "2014,2149", "15");
var enRouteCard = Cards[5];
Cards[6] = new cc_type("JCBCard", "3088,3096,3112,3158,3337,3528", "16");
var JCBCard = Cards[6];
var LuhnCheckSum = Cards[7] = new cc_type();

/*************************************************************************\
Checkcc_number(form)
function called when users click the "check" button.
\*************************************************************************/
function Checkcc_number(form) {
var tmpyear;
if (form.cc_number.value.length == 0) {
alert("Please enter a Card Number.");
form.cc_number.focus();
return;
}
if (form.expdate_year.value.length == 0) {
alert("Please enter the Expiration Year.");
form.expdate_year.focus();
return;
}
if (form.expdate_year.value > 96)
tmpyear = "19" + form.expdate_year.value;
else if (form.expdate_year.value < 21)
tmpyear = "20" + form.expdate_year.value;
else {
alert("The Expiration Year is not valid.");
return;
}
tmpmonth = form.expdate_month.options[form.expdate_month.selectedIndex].value;
// The following line doesn't work in IE3, you need to change it
// to something like "(new cc_type())...".
// if (!cc_type().isExpiryDate(tmpyear, tmpmonth)) {
if (!(new cc_type()).isExpiryDate(tmpyear, tmpmonth)) {
alert("This card has already expired.");
return;
}
card = form.cc_type.options[form.cc_type.selectedIndex].value;
var retval = eval(card + ".checkcc_number(\"" + form.cc_number.value +
"\", " + tmpyear + ", " + tmpmonth + ");");
cardname = "";
if (retval);

// comment this out if used on an order form
//alert("This card number appears to be valid.");

else {
// The cc_number has the valid luhn checksum, but we want to know which
// cc_type it belongs to.
for (var n = 0; n < Cards.size; n++) {
if (Cards[n].checkcc_number(form.cc_number.value, tmpyear, tmpmonth)) {
cardname = Cards[n].getcc_type();
break;
   }
}
if (cardname.length > 0) {
alert("This looks like a " + cardname + " number, not a " + card + " number.");
}
else {
alert("This card number is not valid.");
      }
   }
}
/*************************************************************************\
Object cc_type([String cc_type, String rules, String len, int year,
                                        int month])
cc_type    : type of card, eg: MasterCard, Visa, etc.
rules       : rules of the cc_number, eg: "4", "6011", "34,37".
len         : valid length of cc_number, eg: "16,19", "13,16".
year        : year of expiry date.
month       : month of expiry date.
eg:
var VisaCard = new cc_type("Visa", "4", "16");
var AmExCard = new cc_type("AmEx", "34,37", "15");
\*************************************************************************/
function cc_type() {
var n;
var argv = cc_type.arguments;
var argc = cc_type.arguments.length;

this.objname = "object cc_type";

var tmpcc_type = (argc > 0) ? argv[0] : "CardObject";
var tmprules = (argc > 1) ? argv[1] : "0,1,2,3,4,5,6,7,8,9";
var tmplen = (argc > 2) ? argv[2] : "13,14,15,16,19";

this.setcc_number = setcc_number;  // set cc_number method.
this.setcc_type = setcc_type;  // setcc_type method.
this.setLen = setLen;  // setLen method.
this.setRules = setRules;  // setRules method.
this.setExpiryDate = setExpiryDate;  // setExpiryDate method.

this.setcc_type(tmpcc_type);
this.setLen(tmplen);
this.setRules(tmprules);
if (argc > 4)
this.setExpiryDate(argv[3], argv[4]);

this.checkcc_number = checkcc_number;  // checkcc_number method.
this.getExpiryDate = getExpiryDate;  // getExpiryDate method.
this.getcc_type = getcc_type;  // getcc_type method.
this.iscc_number = iscc_number;  // iscc_number method.
this.isExpiryDate = isExpiryDate;  // isExpiryDate method.
this.luhnCheck = luhnCheck;// luhnCheck method.
return this;
}

/*************************************************************************\
boolean checkcc_number([String cc_number, int year, int month])
return true if cc_number pass the luhncheck and the expiry date is
valid, else return false.
\*************************************************************************/
function checkcc_number() {
var argv = checkcc_number.arguments;
var argc = checkcc_number.arguments.length;
var cc_number = (argc > 0) ? argv[0] : this.cc_number;
var year = (argc > 1) ? argv[1] : this.year;
var month = (argc > 2) ? argv[2] : this.month;

this.setcc_number(cc_number);
this.setExpiryDate(year, month);

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

return true;
}
/*************************************************************************\
String getcc_type()
return the cc_type.
\*************************************************************************/
function getcc_type() {
return this.cc_type;
}
/*************************************************************************\
String getExpiryDate()
return the expiry date.
\*************************************************************************/
function getExpiryDate() {
return this.month + "/" + this.year;
}
/*************************************************************************\
boolean iscc_number([String cc_number])
return true if cc_number pass the luhncheck and the rules, else return
false.
\*************************************************************************/
function iscc_number() {
var argv = iscc_number.arguments;
var argc = iscc_number.arguments.length;
var cc_number = (argc > 0) ? argv[0] : this.cc_number;
if (!this.luhnCheck())
return false;

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

/*************************************************************************\
boolean isExpiryDate([int year, int month])
return true if the date is a valid expiry date,
else 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;
}

/*************************************************************************\
boolean isNum(String argvalue)
return true if argvalue contains only numeric characters,
else return false.
\*************************************************************************/
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;
}

/*************************************************************************\
boolean luhnCheck([String cc_number])
return true if cc_number pass the luhn check else return false.
Reference: http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl
\*************************************************************************/
function luhnCheck() {
var argv = luhnCheck.arguments;
var argc = luhnCheck.arguments.length;

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

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

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

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

/*************************************************************************\
ArrayObject makeArray(int size)
return the array object in the size specified.
\*************************************************************************/
function makeArray(size) {
this.size = size;
return this;
}

/*************************************************************************\
cc_type setcc_number(cc_number)
return the cc_type object.
\*************************************************************************/
function setcc_number(cc_number) {
this.cc_number = cc_number;
return this;
}

/*************************************************************************\
cc_type setcc_type(cc_type)
return the cc_type object.
\*************************************************************************/
function setcc_type(cc_type) {
this.cc_type = cc_type;
return this;
}

/*************************************************************************\
cc_type setExpiryDate(year, month)
return the cc_type object.
\*************************************************************************/
function setExpiryDate(year, month) {
this.year = year;
this.month = month;
return this;
}

/*************************************************************************\
cc_type setLen(len)
return the cc_type object.
\*************************************************************************/
function setLen(len) {
// Create the len array.
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;
}

/*************************************************************************\
cc_type setRules()
return the cc_type object.
\*************************************************************************/
function setRules(rules) {
// Create the rules array.
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;
}
//  End -->
</script>
0
 
LVL 2

Accepted Solution

by:
Adesso21 earned 500 total points
ID: 12408529
Remeber the Validation function I wrote you...

function Validate(FObj) {
     var CType = FObj.cctype[FObj.cctype.selectedIndex].value;
     Cards[CType].setcc_number(FObj.ccnum.value);
     Cards[CType].setExpirationDate(FObj.expyear.value,FObj.expmonth.value);
     var msg = ""
     msg += "\n" + Cards[CType].getcc_type();
     msg += "\n" + Cards[CType].cc_number;
     msg += "\n" + Cards[CType].getExpirationDate();
     msg += "\nYour card validates as " + Cards[CType].checkcc_number();
     alert(msg);
     return false; // Just to stop the page from actually POSTING change to tru if you are happy.
}

change it to the following

function Validate(FObj) {
     var CType = FObj.cctype[FObj.cctype.selectedIndex].value;
     Cards[CType].setcc_number(FObj.ccnum.value);
     Cards[CType].setExpirationDate(FObj.expyear.value,FObj.expmonth.value);
     isValid = Cards[CType].checkcc_number();
     if(!isValid) {
          alert("Your Card is not valid!");
     }
     return isValid;
}
Remeber to call the function in the form with
<form onSubmit="return Validate(this)" action="" method="POST">

If the card is Valid it will return a true that will submit the form, if not, it will prevent the form from submitting.. All that you have to do now is tell the user about the false validation in some way like I showed you in the function.

If there are any error in the script.. the form will also submit... but last I remember the code was error free..

I hope this helps .

Good Luck
Adesso21
0
 
LVL 2

Author Comment

by:Caiapfas
ID: 12415246
ok adesso21..

the return thing worked, but why?

also...having more issues...i dont want to rewrite any of the script

http://www.experts-exchange.com/Programming/Q_21183295.html
0
 
LVL 2

Expert Comment

by:Adesso21
ID: 12426386
Caiapfas

In the form you tell the event handler that it should wait for the return of the funtion before actually proceeding with the Submit.. and in the function you return either a fals or true.. thus if the Event handler, in this case onSubmit, returns a false to the action it fails to actually post the form and visa versa.. make sense.. ?

As for the rest of your problems, I can try and help, but if its logic regarding Credit Cards, I maight not be able to help unless you tell me what you want it to do.. The code I can help you with, and making it do what you want it to do, but I don know how the Credit Card stuf actually works..

Best of Luck
Adesso21

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

SSL stands for “Secure Sockets Layer” and an SSL certificate is a critical component to keeping your website safe, secured, and compliant. Any ecommerce website must have an SSL certificate to ensure the safe handling of sensitive information like…
Every business owner understands the significance of online customer reviews and the impact it can have on sales and revenues. With technology advancing at such a rapid pace, getting online reviews has never been easier, especially when many regions…
This video teaches users how to migrate an existing Wordpress website to a new domain.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now