Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4600)

Unified Diff: components/autofill/core/browser/validation.cc

Issue 2673753005: [Payments] Basic validation in the credit card editor. (Closed)
Patch Set: more tests Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/autofill/core/browser/validation.cc
diff --git a/components/autofill/core/browser/validation.cc b/components/autofill/core/browser/validation.cc
index 15a96e6ab226bfb786a6ed6352523cf53dfc05c4..91e3929d8635de0fc0c9ce55d095a2f3facee153 100644
--- a/components/autofill/core/browser/validation.cc
+++ b/components/autofill/core/browser/validation.cc
@@ -13,7 +13,10 @@
#include "base/time/time.h"
#include "components/autofill/core/browser/credit_card.h"
#include "components/autofill/core/browser/state_names.h"
+#include "components/autofill/core/common/autofill_clock.h"
#include "components/autofill/core/common/autofill_regexes.h"
+#include "grit/components_strings.h"
+#include "ui/base/l10n/l10n_util.h"
namespace autofill {
@@ -174,4 +177,112 @@ bool IsSSN(const base::string16& text) {
return true;
}
+bool IsValidForType(const base::string16& value,
+ ServerFieldType type,
+ base::string16* error_message) {
+ switch (type) {
+ case CREDIT_CARD_NAME_FULL:
+ if (!value.empty())
+ return true;
+
+ if (error_message) {
+ *error_message =
+ l10n_util::GetStringUTF16(IDS_AUTOFILL_VALIDATION_INVALID_NAME);
+ }
+ break;
+
+ case CREDIT_CARD_EXP_MONTH: {
+ CreditCard temp;
+ // Expiration month was in an invalid format.
+ temp.SetExpirationMonthFromString(value, /* app_locale= */ std::string());
+ if (temp.expiration_month() == 0) {
+ if (error_message) {
+ *error_message = l10n_util::GetStringUTF16(
+ IDS_AUTOFILL_VALIDATION_INVALID_CREDIT_CARD_EXPIRATION_MONTH);
+ }
+ break;
+ }
+ return true;
+ }
+
+ case CREDIT_CARD_EXP_2_DIGIT_YEAR:
+ case CREDIT_CARD_EXP_4_DIGIT_YEAR: {
+ CreditCard temp;
+ temp.SetExpirationYearFromString(value);
+ // Expiration year was in an invalid format.
+ if ((temp.expiration_year() == 0) ||
+ (type == CREDIT_CARD_EXP_2_DIGIT_YEAR && value.size() != 2u) ||
+ (type == CREDIT_CARD_EXP_4_DIGIT_YEAR && value.size() != 4u)) {
+ if (error_message) {
+ *error_message = l10n_util::GetStringUTF16(
+ IDS_AUTOFILL_VALIDATION_INVALID_CREDIT_CARD_EXPIRATION_YEAR);
+ }
+ break;
+ }
+
+ base::Time::Exploded now_exploded;
+ AutofillClock::Now().LocalExplode(&now_exploded);
+ if (temp.expiration_year() >= now_exploded.year)
+ return true;
+
+ // If the year is before this year, it's expired.
+ if (error_message) {
+ *error_message = l10n_util::GetStringUTF16(
+ IDS_AUTOFILL_VALIDATION_INVALID_CREDIT_CARD_EXPIRED);
+ }
+ break;
+ }
+
+ case CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR:
+ case CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR: {
+ const base::string16 pattern =
+ type == CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR
+ ? base::UTF8ToUTF16("^[0-9]{1,2}[-/|]?[0-9]{2}$")
+ : base::UTF8ToUTF16("^[0-9]{1,2}[-/|]?[0-9]{4}$");
+
+ CreditCard temp;
+ temp.SetExpirationDateFromString(value);
+
+ // Expiration date was in an invalid format.
+ if (temp.expiration_month() == 0 || temp.expiration_year() == 0 ||
+ !MatchesPattern(value, pattern)) {
+ if (error_message) {
+ *error_message = l10n_util::GetStringUTF16(
+ IDS_AUTOFILL_VALIDATION_INVALID_CREDIT_CARD_EXPIRATION_DATE);
+ }
+ break;
+ }
+
+ // Checking for card expiration.
+ if (IsValidCreditCardExpirationDate(temp.expiration_year(),
+ temp.expiration_month(),
+ AutofillClock::Now())) {
+ return true;
+ }
+
+ if (error_message) {
+ *error_message = l10n_util::GetStringUTF16(
+ IDS_AUTOFILL_VALIDATION_INVALID_CREDIT_CARD_EXPIRED);
+ }
+ break;
+ }
+
+ case CREDIT_CARD_NUMBER:
+ if (IsValidCreditCardNumber(value))
+ return true;
+
+ if (error_message)
+ *error_message = l10n_util::GetStringUTF16(
+ IDS_AUTOFILL_VALIDATION_INVALID_CREDIT_CARD_NUMBER);
+ break;
+
+ default:
+ // Other types such as CREDIT_CARD_TYPE and CREDIT_CARD_VERIFICATION_CODE
+ // are not validated for now.
+ NOTREACHED() << "Attempting to validate unsupported type " << type;
+ break;
+ }
+ return false;
+}
+
} // namespace autofill

Powered by Google App Engine
This is Rietveld 408576698