Advertisement

06.04.2007 at 09:29PM PDT, ID: 22612911
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.0

Credit Card Validation Using Javascript and Coldfusion

Asked by wsessoms in Web Development, JavaScript, Cold Fusion Markup Language

Tags: , , ,

I have the script listed below that validates a credit card number written in javascript. I am not familar with javascript but I am familar with coldfusion. I need to process the results of  the validation so that if the credit card number is valid it can be process by a <CFIF> statement and if it is false it is processed by a <CFELSE> statement. My problem is I don't know how to use the true or false results of the credit card validation script.

I want to be able to send the information via e-mail to a person in the office if the credit card number is valid, If the number is not valid I want to be able to send the user back to the form to try again.

Time is of the essence as I need to have this working ASAP. Your help is appreciated.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
<style type="text/css">
<!--
.style1 {
      font-size: large;
      font-weight: bold;
}
.style10 {font-family: Arial, Helvetica, sans-serif; font-size: 10pt; }
.style11 {font-size: 14pt}
-->
</style>
<SCRIPT LANGUAGE="JavaScript">

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

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

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


else {
// The cardnumber has the valid luhn checksum, but we want to know which
// cardtype it belongs to.
for (var n = 0; n < Cards.size; n++) {
if (Cards[n].checkCardNumber(form.CardNumber.value, tmpyear, tmpmonth)) {
cardname = Cards[n].getCardType();
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 CardType([String cardtype, String rules, String len, int year,
                                        int month])
cardtype    : type of card, eg: MasterCard, Visa, etc.
rules       : rules of the cardnumber, eg: "4", "6011", "34,37".
len         : valid length of cardnumber, eg: "16,19", "13,16".
year        : year of expiry date.
month       : month of expiry date.
eg:
var VisaCard = new CardType("Visa", "4", "16");
var AmExCard = new CardType("AmEx", "34,37", "15");
\*************************************************************************/
function CardType() {
var n;
var argv = CardType.arguments;
var argc = CardType.arguments.length;

this.objname = "object CardType";

var tmpcardtype = (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.setCardNumber = setCardNumber;  // set CardNumber method.
this.setCardType = setCardType;  // setCardType method.
this.setLen = setLen;  // setLen method.
this.setRules = setRules;  // setRules method.
this.setExpiryDate = setExpiryDate;  // setExpiryDate method.

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

this.checkCardNumber = checkCardNumber;  // checkCardNumber method.
this.getExpiryDate = getExpiryDate;  // getExpiryDate method.
this.getCardType = getCardType;  // getCardType method.
this.isCardNumber = isCardNumber;  // isCardNumber method.
this.isExpiryDate = isExpiryDate;  // isExpiryDate method.
this.luhnCheck = luhnCheck;// luhnCheck method.
return this;
}

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

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

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

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

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

/*************************************************************************\
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 CardNumber])
return true if CardNumber 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 CardNumber = argc > 0 ? argv[0] : this.cardnumber;

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

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

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

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

/*************************************************************************\
CardType setCardNumber(cardnumber)
return the CardType object.
\*************************************************************************/
function setCardNumber(cardnumber) {
this.cardnumber = cardnumber;
return this;
}

/*************************************************************************\
CardType setCardType(cardtype)
return the CardType object.
\*************************************************************************/
function setCardType(cardtype) {
this.cardtype = cardtype;
return this;
}

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

/*************************************************************************\
CardType setLen(len)
return the CardType 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;
}

/*************************************************************************\
CardType setRules()
return the CardType 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 check_form() {
if (ThisForm.InvoiceNumber.value == '') {
  alert('You have not entered an Invoice Number');
  return false;
}
if (ThisForm.PatientID.value == '') {
  alert('You have not entered an Patient ID Number');
  return false;
}
if (ThisForm.PatientName.value == '') {
  alert('You have not entered the Patient Name');
  return false;
}
if (ThisForm.ChargeTotal.value == '') {
  alert('You have not entered the Total amount Due');
  return false;
}
if (ThisForm.CardType.value == '') {
  alert('You have not select a Credit Card Type');
  return false;
}
if (ThisForm.ExpMon.value == '') {
  alert('You have not select the Expiration Month');
  return false;
}
if (ThisForm.ExpYear.value == '') {
  alert('You have not select the Expiration Year');
  return false;
}
if (ThisForm.CardNumber.value == '') {
  alert('You have not entered the Card Number');
  return false;
}
if (ThisForm.CardName.value == '') {
  alert('You have not entered the Name on Card');
  return false;
}
if (ThisForm.Phone.value == '') {
  alert('You have not entered a Phone Number');
  return false;
}
if (ThisForm.EmailAddress.value == '') {
  alert('You have not entered a valid E-mail Address');
  return false;
}
}
-->
</script>
</head>
<body>
<div id="header" align="left">
  <table cellpadding="2" width="576" align="center" border="0">
    <tbody>
      <tr>
        <td width="216">&nbsp;</td>
        <td width="346"><div align="center" class="style1">Secure Payment Form<br />
          </div></td>
      </tr>
      <tr>
        <td colspan="2"><span class="style10">This website is secured by GEOTRUST  128-bit SSL certificate thereby   offering the highest level of encryption or security possible. This means you   can rest assured that communications between your browser and this site's web   servers are private and secure</span></td>
      </tr>
    </tbody>
  </table>
</div>
<cfif isdefined('url.action')>
  <br />
  <table width="576" height="81" border="0" align="center" cellpadding="2" bordercolor="#999999">
    <tbody>
      <tr>
        <td bgcolor="#FFFFFF">&nbsp;</td>
        <td colspan="2" nowrap="nowrap" class="style10"><p><strong><br/>
            <span class="style11">Thank you for your payment.</span></strong></p>
          <p><strong><br />
            You will receive a confirmation e-mail in a few moments.<br/>
            </strong></p></td>
      </tr>
    </tbody>
  </table>
  <cfabort>
</cfif>
<div id="Payment_form">
  <form name="ThisForm" action="online_payment.cfm?action=send" method="post" onSubmit="return check_form()">
    <table width="576" border="1" align="center" cellpadding="2" bordercolor="#999999">
      <tbody>
        <tr>
          <td width="8" bgcolor="#FF0000"></td>
          <td width="550" colspan="2" nowrap="nowrap" class="style10"><strong> Red indicates a required field. </strong></td>
        </tr>
      </tbody>
    </table>
    <table width="576" border="1" align="center" cellpadding="2" bordercolor="#999999">
      <tbody>
        <tr>
          <td colspan="3" bgcolor="#FFFF00" class="style10"><strong>Invoice Information</strong></td>
        </tr>
        <tr>
          <td width="8" bgcolor="#FF0000"></td>
          <td width="251" nowrap="nowrap"><span class="style10" id="InvoiceNumber">Invoice Number:</span></td>
          <td nowrap="nowrap"><input name="InvoiceNumber" size="40" maxlength="50" value="" /></td>
        </tr>
        <tr>
          <td width="8" bgcolor="#FF0000"></td>
          <td width="251" nowrap="nowrap"><span class="style10" id="PatientID">Patient ID Number:</span></td>
          <td nowrap="nowrap"><input name="PatientID" maxlength="30" /></td>
        </tr>
        <tr>
          <td width="8" bgcolor="#FF0000"></td>
          <td width="251" nowrap="nowrap"><span class="style10" id="invoicenumbertitle">Patient Name:</span></td>
          <td nowrap="nowrap"><input name="PatientName" maxlength="30" /></td>
        </tr>
      </tbody>
      &nbsp;  &nbsp;
      <tr>
        <td width="8" bgcolor="#FF0000"></td>
        <td width="251" nowrap="nowrap"><span class="style10" id="chargetotaltitle">Total Amount Due:</span></td>
        <td nowrap="nowrap"><span class="style10">
          <input maxlength="10" name="ChargeTotal" />
          ex. 123.51 </span></td>
      </tr>
    </table>
    <table width="576" border="1" align="center" cellpadding="2" bordercolor="#999999">
      <tbody>
        <tr>
          <td colspan="5" bgcolor="#FFFF00"><strong class="style10">Credit Card Information </strong></td>
        </tr>
        <tr>
          <td width="8" bgcolor="#FF0000"></td>
          <td class="style10">Credit Card Type:<br /></td>
          <td colspan="3" class="style10"><select name="CardType">
              <option value="" selected="selected">Select Card</option>
              <option value="MasterCard">MasterCard
              <option value="VisaCard">Visa
              <option value="AmExCard">American Express
              <option value="DinersClubCard">Diners Club
              <option value="DiscoverCard">Discover
              <option value="enRouteCard">enRoute
              <option value="JCBCard">JCB
            </select></td>
        </tr>
      </tbody>
      &nbsp;
      <tr>
        <td width="8" bgcolor="#FF0000"></td>
        <td class="style10"><a title="To swipe a card, place your cursor in the credit card number field, then swipe the card.  The data should appear in the field.">Credit   Card Number: </a></td>
        <td colspan="3" class="style10"><span id="1">
          <input maxlength="300" size="25" name="CardNumber" />
          </span></td>
      </tr>
      <tr>
        <td width="8" bgcolor="#FF0000"></td>
        <td class="style10"><span id="expdatetitle">Expiration Date: </span></td>
        <td colspan="3" class="style10"><select name="ExpMon">
            <option value="" selected="selected">Select Month</option>
            <option value="1">1
            <option value="2">2
            <option value="3">3
            <option value="4">4
            <option value="5">5
            <option value="6">6
            <option value="7">7
            <option value="8">8
            <option value="9">9
            <option value="10">10
            <option value="11">11
            <option value="12">12
          </select>
          /
          <select name="cardyear" size="1" id="ExpYear">
            <option value="" selected="selected">Select Year</option>
            <option value="2007">2007</option>
            <option value="2008">2008</option>
            <option value="2009">2009</option>
            <option value="2010">2010</option>
            <option value="2011">2011</option>
            <option value="2012">2012</option>
            <option value="2013">2013</option>
            <option value="2014">2014</option>
            <option value="2015">2015</option>
            <option value="2016">2016</option>
            <option value="2017">2017</option>
            <option value="2018">2018</option>
            <option value="2019">2019</option>
            <option value="2020">2020</option>
            <option value="2021">2021</option>
            <option value="2022">2022</option>
          </select>
        </td>
      </tr>
      <tr>
        <td width="8" bgcolor="#FF0000"></td>
        <td width="251" nowrap="nowrap" class="style10"><span id="cardnametitle">Name on Card:</span></td>
        <td nowrap="nowrap" class="style10"><input maxlength="30" name="CardName" /></td>
      </tr>
      <tr>
        <td width="8" bgcolor="#FF0000"></td>
        <td width="251" nowrap="nowrap" class="style10"><span id="phonetitle">Phone:</span></td>
        <td nowrap="nowrap" class="style10"><input maxlength="45" size="36" name="Phone" /></td>
      </tr>
      <tr>
        <td width="8" bgcolor="#FF0000"></td>
        <td width="251" nowrap="nowrap" class="style10">E-mail Address: </td>
        <td nowrap="nowrap" class="style10"><input maxlength="45" size="36" name="EmailAddress" /></td>
      </tr>
      </tbody>
     
    </table>
    <table cellpadding="2" width="576" align="center" border="0">
      <tbody>
        <tr>
          <td height="23" align="center">&nbsp;</td>
          <td align="left">&nbsp;</td>
        </tr>
        <tr>
          <td height="38" align="center" valign="top"><p align="left">
              <input type="checkbox" name="debitplan" value="checkbox" />
            </p></td>
          <td align="left" valign="top"><span class="style10">Check here if interested in receiving information on our direct debit plan. A speedy way to pay your weekly invoices, discounts apply on your weekly invoices with this option.</span></td>
        </tr>
        <tr>
          <td height="65" colspan="2" align="center"><p>
              <input name="Submit" type="image" value="Submit" src="images/paybutton_ani.gif" alt="Pay Bill" align="middle" width="100" height="29" OnClick="CheckCardNumber(this.form)" />
            </p></td>
        </tr>
      </tbody>
    </table>
  </form>
</div>
<div>
  <div align="center"><span class="style10"><em>Note: Submitting payment does not   mean your payment has been processed, accepted or approved. </em></span></div>
</div>
<div>
  <div align="center"><span class="style10"><em>You will receive an email   confirmation should you provide your email address.</em></span></div>
</div>
<div align="right">
  <p align="center" class="style10">Privacy Statement </p>
  <p align="center" class="style10">&nbsp;</p>
</div>
<p align="center">&nbsp;</p>
</body>
</html>

Start Free Trial
 
Loading Advertisement...
 
[+][-]06.04.2007 at 11:47PM PDT, ID: 19215002

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Web Development, JavaScript, Cold Fusion Markup Language
Tags: card, credit, coldfusion, validation
Sign Up Now!
Solution Provided By: Rok-Kralj
Participating Experts: 1
Solution Grade: A
 
 
[+][-]06.04.2007 at 11:55PM PDT, ID: 19215039

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.05.2007 at 12:47AM PDT, ID: 19215225

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.05.2007 at 01:06AM PDT, ID: 19215326

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.05.2007 at 03:44AM PDT, ID: 19215916

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.04.2007 at 09:56AM PDT, ID: 19419564

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32