# Credit Card Verification Code

For developers who work with payments and credit card numbers, algorithms for client-size validation are extremely important. Using methods like the Luhn algorithm, it is possible to verify a user-provided credit card number is free of typos with only a few lines of code.

The following tabs contain implementations the Luhn algorithm in five common programming languages. You can pass a credit card number in to any of these functions, and receive a boolean value of true or false depending on whether the card is calculated to be valid by the Luhn algorithm.

```function luhn_test(\$num) {
\$str = '';
foreach( array_reverse( str_split( \$num ) ) as \$i => \$c ) \$str .= (\$i % 2 ? \$c * 2 : \$c );
return array_sum( str_split(\$str) ) % 10 == 0;
}
```
```var LuhnCheck = (function()
{
var luhnArr = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9];
return function(str)
{
var counter = 0;
var incNum;
var odd = false;
var temp = String(str).replace(/[^\d]/g, "");
if ( temp.length == 0)
return false;
for (var i = temp.length-1; i >= 0; --i)
{
incNum = parseInt(temp.charAt(i), 10);
counter += (odd = !odd)? incNum : luhnArr[incNum];
}
return (counter%10 == 0);
}
})();
```
```Imports System.Linq
Function ValidLuhn(value As String)
Return value.Select(Function(c, i) (AscW(c) - 48) << ((value.Length - i - 1) And 1)).Sum(Function(n) If(n > 9, n - 9, n)) Mod 10 = 0
End Function
```
```public static boolean luhnTest(String number){
int s1 = 0, s2 = 0;
String reverse = new StringBuffer(number).reverse().toString();
for(int i = 0 ;i < reverse.length();i++){
int digit = Character.digit(reverse.charAt(i), 10);
if(i % 2 == 0){//this is for odd digits, they are 1-indexed in the algorithm
s1 += digit;
}else{//add 2 * digit for 0-4, add 2 * digit - 9 for 5-9
s2 += 2 * digit;
if(digit >= 5){
s2 -= 9;
}
}
}
return (s1 + s2) % 10 == 0;
}
```
```def luhn(n):
r = [int(ch) for ch in str(n)][::-1]
return (sum(r[0::2]) + sum(sum(divmod(d*2,10)) for d in r[1::2])) % 10 == 0
```
```#include <iostream>
using namespace std;

int toInt(const char c)
{
return c-'0';
}

int confirm( const char *id)
{
bool is_odd_dgt = true;
int s = 0;
const char *cp;

for(cp=id; *cp; cp++);
while(cp > id) {
--cp;
int k = toInt(*cp);
if (is_odd_dgt) {
s += k;
}
else {
s += (k!=9)? (2*k)%9 : 9;
}
is_odd_dgt = !is_odd_dgt;
}
return 0 == s%10;
}

int main( )
{
const char * t_cases[] = {
"49927398716",
"49927398717",
"1234567812345678",
"1234567812345670",
NULL,
};
for ( const char **cp = t_cases; *cp; cp++) {
cout << *cp << ": " << confirm(*cp) << endl;
}
return 0;
}
```

### Credit Card Validation Libraries

For more in-depth applications, you may want to use a full credit card verification library. In addition to verifying the validity of a credit card number via the Luhn algorithm, many libraries exist that use the card numbers' issuer identification number (IIN) to determine the type of card being used.

Our recommended JavaScript credit card validation library is the JQuery Credit Card Validator, open source software which implements both Luhn verification and limited IIN lookups. You can download the library at https://github.com/PawelDecowski/jQuery-CreditCardValidator/. You can see this library in action on our credit card validator tool.

### Regular Expressions for Credit Card Validation

While nowhere near as powerful as the Luhn algorithm for validating properly-formatted credit card numbers, a simple regular expression can be useful for simple verifications. The following regexes can be used:

Disclaimer: Thank you for using CreditCardValidator.org! Please note that while we strive to ensure that our list of credit/debit card IIN/BINs and other payment card data is complete and up to date, we have to provide this resource on an AS-IS basis and cannot guarantee its accuracy.

None of the data you enter on this site will be stored or cached on our servers. All of our credit card verification tools are client-side, so entered data never leaves your browser.

** This Document Provided By CreditCardValidator **
Source: http://www.creditcardvalidator.org/developer