Code Snippets

  

JavaScript Source Code


Welcome to Dream.In.Code
Getting Help is Easy!

Join 131,099 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,136 people online right now. Registration is fast and FREE... Join Now!





LUHN10 (Credit Card) Validation

A function that validates a credit card based on the LUHN10 algorithm. This function does not care if the number is a visa, mastercard, etc. etc.

Submitted By: skyhawk133
Actions:
Rating:
Views: 12,167

Language: JavaScript

Last Modified: March 29, 2005
Instructions: Pass the credit card number to the function, if it's valid true will be returned, if not, false will be returned.

Snippet


  1. /*---------------------------------------------------------------*/
  2. /*                                                               */
  3. /* Function  : isCreditCard()                                           */
  4. /* Purpose   : Check if cc is LUHN10      */
  5. /*                                                                  */
  6. /*                                                               */
  7. /* Parameters:     cc - the cc number                                                       */
  8. /*                                                               */
  9. /* Returns   : boolean                                              */
  10. /*                                                               */
  11. /* Usage     : isCreditCard(cc)                                           */
  12. /*---------------------------------------------------------------*/
  13.  
  14.      function isCreditCard( CC )
  15.      {                         
  16.           if (CC.length > 19)
  17.                return (false);
  18.  
  19.           sum = 0; mul = 1; l = CC.length;
  20.           for (i = 0; i < l; i++)
  21.           {
  22.                digit = CC.substring(l-i-1,l-i);
  23.                tproduct = parseInt(digit ,10)*mul;
  24.                if (tproduct >= 10)
  25.                     sum += (tproduct % 10) + 1;
  26.                else
  27.                     sum += tproduct;
  28.                if (mul == 1)
  29.                     mul++;
  30.                else
  31.                     mul--;
  32.           }
  33.           if ((sum % 10) == 0)
  34.                return (true);
  35.           else
  36.                return (false);
  37.      }
  38.  
  39.  

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month