Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Credit card validation needed! 2

Status
Not open for further replies.

hpadwal

Programmer
Feb 6, 2006
57
GB
Hello all,

does anyone have a credit card validation script that acutally works, i have used a few i found online but there seems to be endless debugging.

The foillowing cards will be used
MasterCard

UK Maestro/Solo

Visa / Visa Electron

Switch

Laser (ROI)

also if you have any scripts for validating

Direct Debit

This info would be great. thank you

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

- Doh

 
hpadwal, this is really something that you'd want to do server-side instead of using javascript. Any user could turn off javascript in their browser to bypass your validation routine. When dealing with secure information like credit card numbers you're really best off validating in a secure environment, and doing it client side with javascript does not fit that criteria.

I see from your profile that you've asked questions in the ASP forum as well, so I'll assume that for this project you use ASP for the server side code. That said, I would ask this question in that forum instead.

Sorry this doesn't really answer your question.....

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Of course, the only real way to validate a credit card number beyond just checking for a POSSIBLE valid number is to either establish your own online service for this, like Paypal and Authorize.net and several other companies have done, or use one of those companies to actually handle the credit card processing.

Lee
 
hpadwal,
I agree with kaht that your validation should be done server-side but still think you would benefit from client-side validation as well. The point being that you want to verify that the fied info is at least of a valid type prior to allowing the form to submit. You still need to validate server-side but at least you are not posting the form then having to re-display the form due to errors.

Here is some code I have been using.
It validates most types of credit cards but whether it works on the ones you specify above I could not say.

Code:
function credit_card(s)
{
  var i, n, c, r, t;
  // First, reverse the string and remove any non-numeric characters.
  r = "";
  for (i = 0; i < s.length; i++) {
    c = parseInt(s.charAt(i), 10);
    if (c >= 0 && c <= 9)
      r = c + r;
  }
  // Check for a bad string.
  if (r.length <= 1)
    return false;
  // Now run through each single digit to create a new string. Even digits are multiplied by two, odd digits are left alone.
  t = "";
  for (i = 0; i < r.length; i++) {
    c = parseInt(r.charAt(i), 10);
    if (i % 2 != 0)
      c *= 2;
    t = t + c;
  }
  // Finally, add up all the single digits in this string.
  n = 0;
  for (i = 0; i < t.length; i++) {
    c = parseInt(t.charAt(i), 10);
    n = n + c;
  }
  // If the resulting sum is an even multiple of ten (but not zero), the card number is good.
  if (n != 0 && n % 10 == 0)
    return true;
  else
    return false;
}

This code works for MasterCard 13 and 16 digit numbers, Visa, American Express & Amex Corporate (15 digit), Diner's Club (14 digit), Discover and JCB.
Here are some test numbers to verify the script with.
Master Card 5431-1111-1111-1111 (16) Characters
Master Card 4222222222222 (13) Characters
VISA 4111111111111111 (16) Characters
VISA 4012888888881881 (16) Characters
American Express 341111111111111 (15) Characters
Amex Corporate 378734493671000 (15) Characters
Dinners Club 38520000023237 (14) Characters
Discover 6011111111111117 (16) Characters
Discover: 6011-6011-6011-6611 (16) Characters
JCB 3530111333300000 (16) Characters



Stamp out, eliminate and abolish redundancy!
 
Thanks for some useful code, and especially for the sample number that will pass the tests.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top