diff options
Diffstat (limited to 'chrome/browser/autofill')
-rw-r--r-- | chrome/browser/autofill/credit_card.cc | 52 | ||||
-rw-r--r-- | chrome/browser/autofill/credit_card.h | 7 |
2 files changed, 29 insertions, 30 deletions
diff --git a/chrome/browser/autofill/credit_card.cc b/chrome/browser/autofill/credit_card.cc index fefab77..7f41b5e 100644 --- a/chrome/browser/autofill/credit_card.cc +++ b/chrome/browser/autofill/credit_card.cc @@ -281,6 +281,31 @@ bool CreditCard::operator!=(const CreditCard& creditcard) const { return !operator==(creditcard); } +// Use the Luhn formula to validate the number. +bool CreditCard::IsCreditCardNumber(const string16& text) { + string16 number; + RemoveChars(text, kCreditCardSeparators.c_str(), &number); + + int sum = 0; + bool odd = false; + string16::reverse_iterator iter; + for (iter = number.rbegin(); iter != number.rend(); ++iter) { + if (!IsAsciiDigit(*iter)) + return false; + + int digit = *iter - '0'; + if (odd) { + digit *= 2; + sum += digit / 10 + digit % 10; + } else { + sum += digit; + } + odd = !odd; + } + + return (sum % 10) == 0; +} + string16 CreditCard::ExpirationMonthAsString() const { if (expiration_month_ == 0) return string16(); @@ -409,33 +434,6 @@ bool CreditCard::IsNameOnCard(const string16& text) const { return StringToLowerASCII(text) == StringToLowerASCII(name_on_card_); } -bool CreditCard::IsCreditCardNumber(const string16& text) { - string16 number; - TrimString(text, kCreditCardSeparators.c_str(), &number); - - // We use the Luhn formula to validate the number; see - // http://www.beachnet.com/~hstiles/cardtype.html and - // http://www.webopedia.com/TERM/L/Luhn_formula.html. - int sum = 0; - bool odd = false; - string16::reverse_iterator iter; - for (iter = number.rbegin(); iter != number.rend(); ++iter) { - if (!IsAsciiDigit(*iter)) - return false; - - int digit = *iter - '0'; - if (odd) { - digit *= 2; - sum += digit / 10 + digit % 10; - } else { - sum += digit; - } - odd = !odd; - } - - return (sum % 10) == 0; -} - bool CreditCard::IsVerificationCode(const string16& text) const { return StringToLowerASCII(text) == StringToLowerASCII(verification_code_); } diff --git a/chrome/browser/autofill/credit_card.h b/chrome/browser/autofill/credit_card.h index 4c16ec7..50bfa3a 100644 --- a/chrome/browser/autofill/credit_card.h +++ b/chrome/browser/autofill/credit_card.h @@ -58,6 +58,10 @@ class CreditCard : public FormGroup { bool operator!=(const CreditCard& creditcard) const; void set_label(const string16& label) { label_ = label; } + // Returns true if |value| is a credit card number. Uses the Luhn formula to + // validate the number. + static bool IsCreditCardNumber(const string16& text); + private: // The month and year are zero if not present. int Expiration4DigitYear() const { return expiration_year_; } @@ -107,9 +111,6 @@ class CreditCard : public FormGroup { // case-insensitive. bool IsNameOnCard(const string16& text) const; - // Uses the Luhn formula to validate the credit card number in |text|. - static bool IsCreditCardNumber(const string16& text); - // Returns true if |text| matches the expiration month of the card. bool IsExpirationMonth(const string16& text) const; |