Solved

Function to check Credit Card algorithm for input field.

Posted on 2006-10-19
6
1,246 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
6 Comments
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 125 total points
Comment Utility
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 125 total points
Comment Utility
 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 125 total points
Comment Utility
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 125 total points
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

7 Experts available now in Live!

Get 1:1 Help Now