Perform the Luhn Algorithm on a given credit card number

This snippet performs the Luhn Algorithm on a provided value in an attempt to determine if it's a valid credit card number

Author: PsychoCoder
Category: C#

Instructions:
Provide the CC number you wish to validate

Need a reference to the System.Text Namespace

Code:
  1. /// <summary>
  2. ///method utilizing the Luhn algorithm (or Luhn formula) to
  3. ///validate the provided credit card number
  4. /// </summary>
  5. /// <param name="value">number we're validating</param>
  6. /// <returns>True, if valid, otherwise false</returns>
  7. public bool DoesPassLuhnAlgorithmCheck(string value)
  8. {
  9. int sum = 0;
  10. int end = 0;
  11.  
  12. try
  13. {
  14. if (string.IsNullOrEmpty(value))
  15. throw new ArgumentException("The input value cannot be null or empty", value);
  16.  
  17. StringBuilder sb = new StringBuilder(value.Length);
  18.  
  19. //perform Luhn algorithm to determine if the value
  20. //provided is a valid cc number
  21. for (int i = 0; i < value.Length; i++)
  22. {
  23. //here we want only the numbers in the value being checked
  24. if (Char.IsDigit(value[i]))
  25. sb.Append(value[i]);
  26. }
  27.  
  28. if (sb.Length > 18 || sb.Length < 15)
  29. return false;
  30.  
  31. //determines whether the end digit will be multiplies by 2
  32. bool multiplyByTwo = false;
  33.  
  34. //now loop through what we have, looping backwards
  35. for (int i = sb.Length - 1; i >= 0; i--)
  36. {
  37. //get each digit one at a time
  38. int digit = Int32.Parse(sb.ToString(i, 1));
  39.  
  40. //if multiplyByTwo then we need to
  41. //multiple digit by 2
  42. if (multiplyByTwo)
  43. {
  44. end = digit * 2;
  45. if (end > 9)
  46. end -= 9;
  47. }
  48. else
  49. //otherwise end is given the value of the end digit
  50. end = digit;
  51.  
  52. //keep a running total
  53. sum += end;
  54.  
  55. //change the value of multiplyByTwo
  56. multiplyByTwo = !multiplyByTwo;
  57. }
  58.  
  59. //use mod 10 on the sum value
  60. int remainder = sum % 10;
  61.  
  62. //if it's 0 (zero) then we got a valid value
  63. //otherwise we dont
  64. return (remainder == 0);
  65. }
  66. catch (ArgumentException ex)
  67. {
  68. return false;
  69. }
  70. }

Copy to IDE: (Highlight)
/// <summary>
///method utilizing the Luhn algorithm (or Luhn formula) to
///validate the provided credit card number
/// </summary>
/// <param name="value">number we're validating</param>
/// <returns>True, if valid, otherwise false</returns>
public bool DoesPassLuhnAlgorithmCheck(string value)
{
    int sum = 0;
    int end = 0;

    try
    {
        if (string.IsNullOrEmpty(value))
            throw new ArgumentException("The input value cannot be null or empty", value);
               
        StringBuilder sb = new StringBuilder(value.Length);

        //perform Luhn algorithm to determine if the value
        //provided is a valid cc number
        for (int i = 0; i < value.Length; i++)
        {
            //here we want only the numbers in the value being checked
            if (Char.IsDigit(value[i]))
                sb.Append(value[i]);
        }

        if (sb.Length > 18 || sb.Length < 15)
            return false;

        //determines whether the end digit will be multiplies by 2
        bool multiplyByTwo = false;

        //now loop through what we have, looping backwards
        for (int i = sb.Length - 1; i >= 0; i--)
        {
            //get each digit one at a time
            int digit = Int32.Parse(sb.ToString(i, 1));

            //if multiplyByTwo then we need to
            //multiple digit by 2
            if (multiplyByTwo)
            {
                end = digit * 2;
                if (end > 9)
                    end -= 9;
            }
            else
                //otherwise end is given the value of the end digit
                end = digit;

            //keep a running total
            sum += end;

            //change the value of multiplyByTwo
            multiplyByTwo = !multiplyByTwo;
        }

        //use mod 10 on the sum value
        int remainder = sum % 10;

        //if it's 0 (zero) then we got a valid value
        //otherwise we dont
        return (remainder == 0);
    }
    catch (ArgumentException ex)
    {
        return false;
    }
}

Highlighting: C#
Date: August 27, 2010, 07:42:01 AM
Views: 1229


(Please Register or Log In to comment)
TheCodeCube - IT Community | Snippets System - Copyright (c) FoxSoft