Credit Card validation with Jquery

前端 未结 3 1055
情深已故
情深已故 2021-01-06 00:09

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?

3条回答
  •  春和景丽
    2021-01-06 01:06

    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);
     }
    

提交回复
热议问题