Question

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

Asked by: Caiapfas

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>

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2004-10-06 at 09:37:14ID21158218
Tags

card

,

vaildator

,

credit

,

vercation

Topics

Miscellaneous Web Development

,

E-Commerce

Participating Experts
1
Points
500
Comments
12

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Credit Card and Expiry validation
    Does anyone have a credit card validation javascript function?
  2. Argv[] , argc Quesstion
    I am trying to access argv[], argc in a function i am using but it wont compile, it keeps giving me error, 'argv, argc undeclared' int processRows(int i) { int rowNumber; while (i < argc) { if (*argv == '-') { /* the next arguement, so return */ ...
  3. argc, argv
    Are argc, argv local to main

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: Adesso21Posted on 2004-10-06 at 11:46:02ID: 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

 

by: CaiapfasPosted on 2004-10-06 at 11:52:09ID: 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

 

by: Adesso21Posted on 2004-10-06 at 11:57:43ID: 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

 

by: CaiapfasPosted on 2004-10-06 at 12:04:59ID: 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

 

by: Adesso21Posted on 2004-10-06 at 12:51:19ID: 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>

 

by: Adesso21Posted on 2004-10-06 at 12:53:29ID: 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 ..

 

by: CaiapfasPosted on 2004-10-18 at 11:35:54ID: 12340825

still not working, returns all as flase..and yes i'm using really card..lol

 

by: Adesso21Posted on 2004-10-18 at 12:03:17ID: 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

 

by: CaiapfasPosted on 2004-10-25 at 14:33:19ID: 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>

 

by: Adesso21Posted on 2004-10-26 at 01:11:13ID: 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

 

by: CaiapfasPosted on 2004-10-26 at 13:21:04ID: 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

 

by: Adesso21Posted on 2004-10-27 at 12:54:23ID: 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

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...