| 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 "components/autofill/browser/validation.h" | 5 #include "components/autofill/browser/validation.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/time.h" | 9 #include "base/time.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "components/autofill/browser/autofill_regexes.h" | 11 #include "components/autofill/browser/autofill_regexes.h" |
| 12 #include "components/autofill/browser/credit_card.h" | 12 #include "components/autofill/browser/credit_card.h" |
| 13 | 13 |
| 14 namespace autofill { | 14 namespace autofill { |
| 15 | 15 |
| 16 bool IsValidCreditCardExpirationDate(const string16& year, | 16 bool IsValidCreditCardExpirationDate(const base::string16& year, |
| 17 const string16& month, | 17 const base::string16& month, |
| 18 const base::Time& now) { | 18 const base::Time& now) { |
| 19 string16 year_cleaned, month_cleaned; | 19 base::string16 year_cleaned, month_cleaned; |
| 20 TrimWhitespace(year, TRIM_ALL, &year_cleaned); | 20 TrimWhitespace(year, TRIM_ALL, &year_cleaned); |
| 21 TrimWhitespace(month, TRIM_ALL, &month_cleaned); | 21 TrimWhitespace(month, TRIM_ALL, &month_cleaned); |
| 22 if (year_cleaned.length() != 4) | 22 if (year_cleaned.length() != 4) |
| 23 return false; | 23 return false; |
| 24 | 24 |
| 25 base::Time::Exploded now_exploded; | 25 base::Time::Exploded now_exploded; |
| 26 now.LocalExplode(&now_exploded); | 26 now.LocalExplode(&now_exploded); |
| 27 size_t current_year = size_t(now_exploded.year); | 27 size_t current_year = size_t(now_exploded.year); |
| 28 size_t current_month = size_t(now_exploded.month); | 28 size_t current_month = size_t(now_exploded.month); |
| 29 | 29 |
| 30 size_t cc_year; | 30 size_t cc_year; |
| 31 if (!base::StringToSizeT(year_cleaned, &cc_year)) | 31 if (!base::StringToSizeT(year_cleaned, &cc_year)) |
| 32 return false; | 32 return false; |
| 33 if (cc_year < current_year) | 33 if (cc_year < current_year) |
| 34 return false; | 34 return false; |
| 35 | 35 |
| 36 size_t cc_month; | 36 size_t cc_month; |
| 37 if (!base::StringToSizeT(month_cleaned, &cc_month)) | 37 if (!base::StringToSizeT(month_cleaned, &cc_month)) |
| 38 return false; | 38 return false; |
| 39 if (cc_year == current_year && cc_month < current_month) | 39 if (cc_year == current_year && cc_month < current_month) |
| 40 return false; | 40 return false; |
| 41 | 41 |
| 42 return true; | 42 return true; |
| 43 } | 43 } |
| 44 | 44 |
| 45 bool IsValidCreditCardNumber(const string16& text) { | 45 bool IsValidCreditCardNumber(const base::string16& text) { |
| 46 string16 number = CreditCard::StripSeparators(text); | 46 base::string16 number = CreditCard::StripSeparators(text); |
| 47 | 47 |
| 48 // Credit card numbers are at most 19 digits in length [1]. 12 digits seems to | 48 // Credit card numbers are at most 19 digits in length [1]. 12 digits seems to |
| 49 // be a fairly safe lower-bound [2]. | 49 // be a fairly safe lower-bound [2]. |
| 50 // [1] http://www.merriampark.com/anatomycc.htm | 50 // [1] http://www.merriampark.com/anatomycc.htm |
| 51 // [2] http://en.wikipedia.org/wiki/Bank_card_number | 51 // [2] http://en.wikipedia.org/wiki/Bank_card_number |
| 52 const size_t kMinCreditCardDigits = 12; | 52 const size_t kMinCreditCardDigits = 12; |
| 53 const size_t kMaxCreditCardDigits = 19; | 53 const size_t kMaxCreditCardDigits = 19; |
| 54 if (number.size() < kMinCreditCardDigits || | 54 if (number.size() < kMinCreditCardDigits || |
| 55 number.size() > kMaxCreditCardDigits) | 55 number.size() > kMaxCreditCardDigits) |
| 56 return false; | 56 return false; |
| 57 | 57 |
| 58 // Use the Luhn formula [3] to validate the number. | 58 // Use the Luhn formula [3] to validate the number. |
| 59 // [3] http://en.wikipedia.org/wiki/Luhn_algorithm | 59 // [3] http://en.wikipedia.org/wiki/Luhn_algorithm |
| 60 int sum = 0; | 60 int sum = 0; |
| 61 bool odd = false; | 61 bool odd = false; |
| 62 for (string16::reverse_iterator iter = number.rbegin(); | 62 for (base::string16::reverse_iterator iter = number.rbegin(); |
| 63 iter != number.rend(); | 63 iter != number.rend(); |
| 64 ++iter) { | 64 ++iter) { |
| 65 if (!IsAsciiDigit(*iter)) | 65 if (!IsAsciiDigit(*iter)) |
| 66 return false; | 66 return false; |
| 67 | 67 |
| 68 int digit = *iter - '0'; | 68 int digit = *iter - '0'; |
| 69 if (odd) { | 69 if (odd) { |
| 70 digit *= 2; | 70 digit *= 2; |
| 71 sum += digit / 10 + digit % 10; | 71 sum += digit / 10 + digit % 10; |
| 72 } else { | 72 } else { |
| 73 sum += digit; | 73 sum += digit; |
| 74 } | 74 } |
| 75 odd = !odd; | 75 odd = !odd; |
| 76 } | 76 } |
| 77 | 77 |
| 78 return (sum % 10) == 0; | 78 return (sum % 10) == 0; |
| 79 } | 79 } |
| 80 | 80 |
| 81 bool IsValidCreditCardSecurityCode(const string16& text) { | 81 bool IsValidCreditCardSecurityCode(const base::string16& text) { |
| 82 if (text.size() < 3U || text.size() > 4U) | 82 if (text.size() < 3U || text.size() > 4U) |
| 83 return false; | 83 return false; |
| 84 | 84 |
| 85 for (string16::const_iterator iter = text.begin(); | 85 for (base::string16::const_iterator iter = text.begin(); |
| 86 iter != text.end(); | 86 iter != text.end(); |
| 87 ++iter) { | 87 ++iter) { |
| 88 if (!IsAsciiDigit(*iter)) | 88 if (!IsAsciiDigit(*iter)) |
| 89 return false; | 89 return false; |
| 90 } | 90 } |
| 91 return true; | 91 return true; |
| 92 } | 92 } |
| 93 | 93 |
| 94 bool IsValidCreditCardSecurityCode(const string16& code, | 94 bool IsValidCreditCardSecurityCode(const base::string16& code, |
| 95 const string16& number) { | 95 const base::string16& number) { |
| 96 CreditCard card; | 96 CreditCard card; |
| 97 card.SetRawInfo(CREDIT_CARD_NUMBER, number); | 97 card.SetRawInfo(CREDIT_CARD_NUMBER, number); |
| 98 size_t required_length = 3; | 98 size_t required_length = 3; |
| 99 if (card.type() == kAmericanExpressCard) | 99 if (card.type() == kAmericanExpressCard) |
| 100 required_length = 4; | 100 required_length = 4; |
| 101 | 101 |
| 102 return code.length() == required_length; | 102 return code.length() == required_length; |
| 103 } | 103 } |
| 104 | 104 |
| 105 bool IsValidEmailAddress(const string16& text) { | 105 bool IsValidEmailAddress(const base::string16& text) { |
| 106 // E-Mail pattern as defined by the WhatWG. (4.10.7.1.5 E-Mail state) | 106 // E-Mail pattern as defined by the WhatWG. (4.10.7.1.5 E-Mail state) |
| 107 const string16 kEmailPattern = ASCIIToUTF16( | 107 const base::string16 kEmailPattern = ASCIIToUTF16( |
| 108 "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@" | 108 "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@" |
| 109 "[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$"); | 109 "[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$"); |
| 110 return MatchesPattern(text, kEmailPattern); | 110 return MatchesPattern(text, kEmailPattern); |
| 111 } | 111 } |
| 112 | 112 |
| 113 bool IsValidZip(const string16& value) { | 113 bool IsValidZip(const base::string16& value) { |
| 114 const string16 kZipPattern = ASCIIToUTF16("^\\d{5}(-\\d{4})?$"); | 114 const base::string16 kZipPattern = ASCIIToUTF16("^\\d{5}(-\\d{4})?$"); |
| 115 return MatchesPattern(value, kZipPattern); | 115 return MatchesPattern(value, kZipPattern); |
| 116 } | 116 } |
| 117 | 117 |
| 118 } // namespace autofill | 118 } // namespace autofill |
| OLD | NEW |