Chat LIVE With Programming Experts! There Are 23 Online Right Now...

 

Code Snippets

  

JavaScript Source Code


Welcome to Dream.In.Code
Become an Expert!

Join 244,131 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,351 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: 14,641

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!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month