5

I'm trying to validate credit card numbers with jQuery but i dont want to use the validation plugin , is there any other plugin for doing this?

2
  • 4
    What's wrong with the validation plugin?
    – Bartek
    Nov 6, 2009 at 20:11
  • 1
    Yeah? The fact that Google doesn't really returns anything obvious rather than validation plugin is good enough hint
    – Bostone
    Nov 6, 2009 at 20:15

3 Answers 3

11

http://en.wikipedia.org/wiki/Luhn_algorithm

You could minimize this below into a very small footprint in your code.

function isCreditCard( CC )
 {                        
      if (CC.length > 19)
           return (false);

      sum = 0; mul = 1; l = CC.length;
      for (i = 0; i < l; i++)
      {
           digit = CC.substring(l-i-1,l-i);
           tproduct = parseInt(digit ,10)*mul;
           if (tproduct >= 10)
                sum += (tproduct % 10) + 1;
           else
                sum += tproduct;
           if (mul == 1)
                mul++;
           else
                mul--;
      }
      if ((sum % 10) == 0)
           return (true);
      else
           return (false);
 }
2
  • 1
    +1 - Luhn would be the only way to 'validate' it on the client side. Nov 6, 2009 at 20:18
  • The Luhn check is really only useful for spotting transposition errors. You would also need to check the issuer identification range (first six digits of the card number) is valid.
    – PaulG
    Nov 6, 2009 at 20:23
2

If you want to be certain that its a valid card number, you'll need to check much more than the Luhn digit.

The Luhn digit is only intended for checking transposition errors, and can easily be spoofed with numbers such as 22222222222222222

The first six digits of the card number should be checked. These digits are known as the issuer identification number, and can be used to ensure that the number is within a recognised range. Unfortunately you'll struggle to find a comprehensive list of IIN's, not least because issuers are constantly adding and removing ranges. Wikipedia has a very basic overview though: Bank card number

If you can determine the card type from the IIN, you can then have a stab at validating the length of the number also.

Unless you have very good reason, its much easier to use something like a validation plug-in.

0

I recommend you to use Regular expression... You can check my repository I have a code snippet to validate Visa, Master card, Discovery, american express and the Security code

https://github.com/leojavier/RegexCollection


Remember... just because you limit the amount of digits doesn't mean it will be right.

for instance:
- All Visa card numbers start with a 4. New cards have 16 digits.
- All MasterCard numbers start with the numbers 51 through 55. All have 16 digits. and so on...

The index.html is a sample, to show you how you can use it! Feel free to write me if you have any questions :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.