Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Function to check Credit Card algorithm for input field.

Posted on 2006-10-19
6
Medium Priority
?
1,264 Views
Last Modified: 2008-02-01
I wanted to check the credit card number on a form before submiting. What function can I run on the input before submission so that the input has something like onblur or onchange or something. Also any way to check the CVV number too or is there no algorithm for that? Thanks in advance for all the help.
0
Comment
Question by:bemara57
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 500 total points
ID: 17769890
bemara57,

I would use the onblur event for the input field or have it part of script that is run with the form's onsubmit event.

I have a regular expression you can use to test numbers for major credit cards.  You could use it in JavaScript with Match().  In the example below replace object with the name of your object.

if (object.match(/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{14}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/)) {
   
} else {
   
}

Credit for the regular expression goes to RegEx Buddy's library.  There are some cases where I don't mind code made by another.  :)  Let me know if you have any questions or need more information.

b0lsc0tt
0
 
LVL 41

Assisted Solution

by:HonorGod
HonorGod earned 500 total points
ID: 17770994
 Use the Luhn check, as described here:
http://www.bilkent.edu.tr/~berkant/security/hack/luhn_check.html
0
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 500 total points
ID: 17772540
And JavaScript is here ;-)

<html>
<head>
<title>Zvonko &#42;</title>
<script>

function checkCardNum(theField){
  var theNum = theField.value;
  var check = 0;
  for(var i=0;i<theNum.length;i++){
    var num = theNum.substr(i,1);
    num *= ((i+theNum.length)%2)?1:2;
    check += (num>9)?num%10+1:num;
  }
  if(check%10){
    alert("Wrong CreditCard number.");
    theField.select();
    theField.focus();
    return false;
  }
  return true;
}


</script>
</head>
<body>
<form onSubmit="return checkCardNum(this.cardNumber)">
<input type=text name="cardNumber" size="16" maxLength="16" value="446-667-651">
<input type=submit>
</form>
</body>
</html>



0
 
LVL 23

Assisted Solution

by:rama_krishna580
rama_krishna580 earned 500 total points
ID: 17773770
Hi,

The code handles the different cases for cards of even length and cards for odd length (American Express) with the same code. That part is probably the most confusing part of the code when looking at it.

function isValidCreditCard(type, ccnum) {
   if (type == "Visa") {
      // Visa: length 16, prefix 4, dashes optional.
      var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "MC") {
      // Mastercard: length 16, prefix 51-55, dashes optional.
      var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "Disc") {
      // Discover: length 16, prefix 6011, dashes optional.
      var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "AmEx") {
      // American Express: length 15, prefix 34 or 37.
      var re = /^3[4,7]\d{13}$/;
   } else if (type == "Diners") {
      // Diners: length 14, prefix 30, 36, or 38.
      var re = /^3[0,6,8]\d{12}$/;
   }
   if (!re.test(ccnum)) return false;
   // Remove all dashes for the checksum checks to eliminate negative numbers
   ccnum = ccnum.split("-").join("");
   // Checksum ("Mod 10")
   // Add even digits in even length strings or odd digits in odd length strings.
   var checksum = 0;
   for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
      checksum += parseInt(ccnum.charAt(i-1));
   }
   // Analyze odd digits in even length strings or even digits in odd length strings.
   for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
      var digit = parseInt(ccnum.charAt(i-1)) * 2;
      if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
   }
   if ((checksum % 10) == 0) return true; else return false;
}

Note that the first character in a string is charAt(0), so that is why 1 is subtracted from the value of i all the time. For the first pass, if the length is even, then the length mod 2 ("mod" is a mathematical term that says to return the remainder of the first number divided by the second number, so 5 mod 2 is 1 and 6 mod 2 is 0. % is the mod operator in JavaScript) results in 0. So 2 minus 0 is 2, so the value of i will walk through the even numbered digits. In the first pass for strings of odd length, the length mod 2 is 1, so 2 minus 1 is 1, and the value of i will walk through the odd numbered digits.

R.K
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
WooCommerce is becoming the most powerful e-commerce plugin for Wordpress. And why not. The platform comprises of numerous core plugins that may come in handy, powerful options to make your website development task much easier.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

609 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