LuhnRule

public class LuhnRule : RuleType

LuhnRule presents the luhn requirement that a credit card number should follow.

Version

1.9.6

Date

14/09/22

Author

Adamas
  • Initialize the rule

    Declaration

    Swift

    public init(message: String)

    Parameters

    message

    The error message to apply.

  • Run a check against a given credit card number to ensure it passes the Luhn’s check. The code was adapted from a gist And the logic of the gist was checked against the wikipedia page. The logic in a short form is:

    1. Reverse the order of the digits in the number.
    2. Take the first, third, … and every other odd digit in the reversed digits and sum them to form the partial sum s1
    3. Taking the second, fourth … and every other even digit in the reversed digits: a.Multiply each digit by two and sum the digits if the answer is greater than nine to form partial sums for the even digits b. Sum the partial sums of the even digits to form s2

    Declaration

    Swift

    public func isValid(value: String?) -> String?