| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/autofill/core/browser/validation.h" | 5 #include "components/autofill/core/browser/validation.h" |
| 6 | 6 |
| 7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/strings/string_piece.h" | 8 #include "base/strings/string_piece.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 if (cc_year == current_year && cc_month < current_month) | 50 if (cc_year == current_year && cc_month < current_month) |
| 51 return false; | 51 return false; |
| 52 | 52 |
| 53 return true; | 53 return true; |
| 54 } | 54 } |
| 55 | 55 |
| 56 bool IsValidCreditCardNumber(const base::string16& text) { | 56 bool IsValidCreditCardNumber(const base::string16& text) { |
| 57 base::string16 number = CreditCard::StripSeparators(text); | 57 base::string16 number = CreditCard::StripSeparators(text); |
| 58 | 58 |
| 59 // Credit card numbers are at most 19 digits in length [1]. 12 digits seems to | 59 // Credit card numbers are at most 19 digits in length [1]. 12 digits seems to |
| 60 // be a fairly safe lower-bound [2]. | 60 // be a fairly safe lower-bound [2]. Specific card issuers have more rigidly |
| 61 // defined sizes. |
| 61 // [1] http://www.merriampark.com/anatomycc.htm | 62 // [1] http://www.merriampark.com/anatomycc.htm |
| 62 // [2] http://en.wikipedia.org/wiki/Bank_card_number | 63 // [2] http://en.wikipedia.org/wiki/Bank_card_number |
| 63 const size_t kMinCreditCardDigits = 12; | 64 const std::string type = CreditCard::GetCreditCardType(text); |
| 64 const size_t kMaxCreditCardDigits = 19; | 65 if (type == kAmericanExpressCard && number.size() != 15) |
| 65 if (number.size() < kMinCreditCardDigits || | 66 return false; |
| 66 number.size() > kMaxCreditCardDigits) | 67 if (type == kDinersCard && number.size() != 14) |
| 68 return false; |
| 69 if (type == kDiscoverCard && number.size() != 16) |
| 70 return false; |
| 71 if (type == kJCBCard && number.size() != 16) |
| 72 return false; |
| 73 if (type == kMasterCard && number.size() != 16) |
| 74 return false; |
| 75 if (type == kVisaCard && number.size() != 13 && number.size() != 16) |
| 76 return false; |
| 77 if (type == kGenericCard && (number.size() < 12 || number.size() > 19)) |
| 67 return false; | 78 return false; |
| 68 | 79 |
| 69 // Use the Luhn formula [3] to validate the number. | 80 // Use the Luhn formula [3] to validate the number. |
| 70 // [3] http://en.wikipedia.org/wiki/Luhn_algorithm | 81 // [3] http://en.wikipedia.org/wiki/Luhn_algorithm |
| 71 int sum = 0; | 82 int sum = 0; |
| 72 bool odd = false; | 83 bool odd = false; |
| 73 for (base::string16::reverse_iterator iter = number.rbegin(); | 84 for (base::string16::reverse_iterator iter = number.rbegin(); |
| 74 iter != number.rend(); | 85 iter != number.rend(); |
| 75 ++iter) { | 86 ++iter) { |
| 76 if (!IsAsciiDigit(*iter)) | 87 if (!IsAsciiDigit(*iter)) |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 number_string.begin() + 9), | 198 number_string.begin() + 9), |
| 188 &serial) | 199 &serial) |
| 189 || serial == 0) { | 200 || serial == 0) { |
| 190 return false; | 201 return false; |
| 191 } | 202 } |
| 192 | 203 |
| 193 return true; | 204 return true; |
| 194 } | 205 } |
| 195 | 206 |
| 196 } // namespace autofill | 207 } // namespace autofill |
| OLD | NEW |