Chromium Code Reviews| Index: chrome/browser/autofill/validation.cc |
| diff --git a/chrome/browser/autofill/validation.cc b/chrome/browser/autofill/validation.cc |
| index ad34b312f7948b54991957b3e2cd356ba3394675..a608e3aed722dd778954cec72a97d6c299353396 100644 |
| --- a/chrome/browser/autofill/validation.cc |
| +++ b/chrome/browser/autofill/validation.cc |
| @@ -5,6 +5,9 @@ |
| #include "chrome/browser/autofill/validation.h" |
| #include "base/string_util.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/time.h" |
| +#include "base/utf_string_conversions.h" |
| #include "chrome/browser/autofill/credit_card.h" |
| namespace autofill { |
| @@ -45,4 +48,71 @@ bool IsValidCreditCardNumber(const string16& text) { |
| return (sum % 10) == 0; |
| } |
| +bool IsValidCreditCardExpirationYear(const string16& text, |
| + size_t num_digits, |
| + const base::Time& now) { |
| + return IsValidCreditCardExpirationDate(ASCIIToUTF16("12/")+text, |
| + num_digits, |
| + now); |
| +} |
| + |
| +bool IsValidCreditCardExpirationDate(const string16& text, |
| + size_t num_digits, |
| + const base::Time& now) { |
| + DCHECK(num_digits == 2 || num_digits == 4); |
| + const char16 kSeparatorSlash = '/'; |
| + const char16 kSeparatorDash = '-'; |
| + |
| + // A date string has |num_digits| chars for year, 2 for month, 1 separator. |
| + if (text.length() != num_digits + 3) |
| + return false; |
| + |
| + string16 year_text, month_text; |
| + 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
|
| + if (separator == 2) { |
| + 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
|
| + month_text = text.substr(0,2); |
| + } else { |
| + separator = text.find(kSeparatorDash); |
| + if (separator != num_digits) |
| + return false; |
| + year_text = text.substr(0, num_digits); |
| + month_text = text.substr(separator+1); |
| + } |
| + |
| + base::Time::Exploded now_exploded; |
| + now.LocalExplode(&now_exploded); |
| + size_t current_year = size_t(now_exploded.year); |
| + size_t current_month = size_t(now_exploded.month); |
| + if (num_digits == 2) |
| + current_year = current_year % 100; |
| + |
| + size_t year; |
| + if (!base::StringToSizeT(year_text, &year)) |
| + return false; |
| + if (year < current_year) |
| + return false; |
| + |
| + size_t month; |
| + if (!base::StringToSizeT(month_text, &month)) |
| + return false; |
| + if (year == current_year && month < current_month) |
| + return false; |
| + |
| + return true; |
| +} |
| + |
| +bool IsValidCreditCardCSC(const string16& text) { |
| + if (text.size() < 3U || text.size() > 4U) |
| + return false; |
| + |
| + for (string16::const_iterator iter = text.begin(); |
| + iter != text.end(); |
| + ++iter) { |
| + if (!IsAsciiDigit(*iter)) |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| } // namespace autofill |