OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "chrome/browser/autofill/validation.h" | 5 #include "chrome/browser/autofill/validation.h" |
6 | 6 |
7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
8 #include "base/strings/string_number_conversions.h" | |
9 #include "base/time.h" | |
10 #include "base/utf_string_conversions.h" | |
8 #include "chrome/browser/autofill/credit_card.h" | 11 #include "chrome/browser/autofill/credit_card.h" |
9 | 12 |
10 namespace autofill { | 13 namespace autofill { |
11 | 14 |
12 bool IsValidCreditCardNumber(const string16& text) { | 15 bool IsValidCreditCardNumber(const string16& text) { |
13 string16 number = CreditCard::StripSeparators(text); | 16 string16 number = CreditCard::StripSeparators(text); |
14 | 17 |
15 // Credit card numbers are at most 19 digits in length [1]. 12 digits seems to | 18 // Credit card numbers are at most 19 digits in length [1]. 12 digits seems to |
16 // be a fairly safe lower-bound [2]. | 19 // be a fairly safe lower-bound [2]. |
17 // [1] http://www.merriampark.com/anatomycc.htm | 20 // [1] http://www.merriampark.com/anatomycc.htm |
(...skipping 20 matching lines...) Expand all Loading... | |
38 sum += digit / 10 + digit % 10; | 41 sum += digit / 10 + digit % 10; |
39 } else { | 42 } else { |
40 sum += digit; | 43 sum += digit; |
41 } | 44 } |
42 odd = !odd; | 45 odd = !odd; |
43 } | 46 } |
44 | 47 |
45 return (sum % 10) == 0; | 48 return (sum % 10) == 0; |
46 } | 49 } |
47 | 50 |
51 bool IsValidCreditCardExpirationYear(const string16& text, | |
52 size_t num_digits, | |
53 const base::Time& now) { | |
54 return IsValidCreditCardExpirationDate(ASCIIToUTF16("12/")+text, | |
55 num_digits, | |
56 now); | |
57 } | |
58 | |
59 bool IsValidCreditCardExpirationDate(const string16& text, | |
60 size_t num_digits, | |
61 const base::Time& now) { | |
62 DCHECK(num_digits == 2 || num_digits == 4); | |
63 const char16 kSeparatorSlash = '/'; | |
64 const char16 kSeparatorDash = '-'; | |
65 | |
66 // A date string has |num_digits| chars for year, 2 for month, 1 separator. | |
67 if (text.length() != num_digits + 3) | |
68 return false; | |
69 | |
70 string16 year_text, month_text; | |
71 size_t separator = text.find(kSeparatorSlash); | |
Evan Stade
2013/02/08 15:48:14
Seems like you're doing a lot of work to parse som
groby-ooo-7-16
2013/02/09 00:01:49
Killing it, since we don't validate fields of that
| |
72 if (separator == 2) { | |
73 year_text = text.substr(separator+1); | |
Evan Stade
2013/02/08 15:48:14
Spaces around operators.
groby-ooo-7-16
2013/02/09 00:01:49
It's dead, Jim.
On 2013/02/08 15:48:14, Evan Stade
| |
74 month_text = text.substr(0,2); | |
75 } else { | |
76 separator = text.find(kSeparatorDash); | |
77 if (separator != num_digits) | |
78 return false; | |
79 year_text = text.substr(0, num_digits); | |
80 month_text = text.substr(separator+1); | |
81 } | |
82 | |
83 base::Time::Exploded now_exploded; | |
84 now.LocalExplode(&now_exploded); | |
85 size_t current_year = size_t(now_exploded.year); | |
86 size_t current_month = size_t(now_exploded.month); | |
87 if (num_digits == 2) | |
88 current_year = current_year % 100; | |
89 | |
90 size_t year; | |
91 if (!base::StringToSizeT(year_text, &year)) | |
92 return false; | |
93 if (year < current_year) | |
94 return false; | |
95 | |
96 size_t month; | |
97 if (!base::StringToSizeT(month_text, &month)) | |
98 return false; | |
99 if (year == current_year && month < current_month) | |
100 return false; | |
101 | |
102 return true; | |
103 } | |
104 | |
105 bool IsValidCreditCardCSC(const string16& text) { | |
106 if (text.size() < 3U || text.size() > 4U) | |
107 return false; | |
108 | |
109 for (string16::const_iterator iter = text.begin(); | |
110 iter != text.end(); | |
111 ++iter) { | |
112 if (!IsAsciiDigit(*iter)) | |
113 return false; | |
114 } | |
115 return true; | |
116 } | |
117 | |
48 } // namespace autofill | 118 } // namespace autofill |
OLD | NEW |