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
3 // found in the LICENSE file.
5 #include "components/autofill/core/browser/validation.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/string_piece.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/time/time.h"
12 #include "components/autofill/core/browser/autofill_regexes.h"
13 #include "components/autofill/core/browser/credit_card.h"
14 #include "components/autofill/core/browser/state_names.h"
16 using base::StringPiece16;
20 // The separator characters for SSNs.
21 const base::char16 kSSNSeparators[] = {' ', '-', 0};
27 bool IsValidCreditCardExpirationDate(const base::string16& year,
28 const base::string16& month,
29 const base::Time& now) {
30 base::string16 year_cleaned, month_cleaned;
31 TrimWhitespace(year, TRIM_ALL, &year_cleaned);
32 TrimWhitespace(month, TRIM_ALL, &month_cleaned);
33 if (year_cleaned.length() != 4)
37 if (!base::StringToInt(year_cleaned, &cc_year))
41 if (!base::StringToInt(month_cleaned, &cc_month))
44 return IsValidCreditCardExpirationDate(cc_year, cc_month, now);
47 bool IsValidCreditCardExpirationDate(int year,
49 const base::Time& now) {
50 base::Time::Exploded now_exploded;
51 now.LocalExplode(&now_exploded);
53 if (year < now_exploded.year)
56 if (year == now_exploded.year && month < now_exploded.month)
62 bool IsValidCreditCardNumber(const base::string16& text) {
63 base::string16 number = CreditCard::StripSeparators(text);
65 // Credit card numbers are at most 19 digits in length [1]. 12 digits seems to
66 // be a fairly safe lower-bound [2]. Specific card issuers have more rigidly
68 // [1] http://www.merriampark.com/anatomycc.htm
69 // [2] http://en.wikipedia.org/wiki/Bank_card_number
70 const std::string type = CreditCard::GetCreditCardType(text);
71 if (type == kAmericanExpressCard && number.size() != 15)
73 if (type == kDinersCard && number.size() != 14)
75 if (type == kDiscoverCard && number.size() != 16)
77 if (type == kJCBCard && number.size() != 16)
79 if (type == kMasterCard && number.size() != 16)
81 if (type == kUnionPay && (number.size() < 16 || number.size() > 19))
83 if (type == kVisaCard && number.size() != 13 && number.size() != 16)
85 if (type == kGenericCard && (number.size() < 12 || number.size() > 19))
88 // Unlike all the other supported types, UnionPay cards lack Luhn checksum
90 if (type == kUnionPay)
93 // Use the Luhn formula [3] to validate the number.
94 // [3] http://en.wikipedia.org/wiki/Luhn_algorithm
97 for (base::string16::reverse_iterator iter = number.rbegin();
98 iter != number.rend();
100 if (!IsAsciiDigit(*iter))
103 int digit = *iter - '0';
106 sum += digit / 10 + digit % 10;
113 return (sum % 10) == 0;
116 bool IsValidCreditCardSecurityCode(const base::string16& text) {
117 if (text.size() < 3U || text.size() > 4U)
120 for (base::string16::const_iterator iter = text.begin();
123 if (!IsAsciiDigit(*iter))
129 bool IsValidCreditCardSecurityCode(const base::string16& code,
130 const base::string16& number) {
131 std::string type = CreditCard::GetCreditCardType(number);
132 size_t required_length = 3;
133 if (type == kAmericanExpressCard)
136 return code.length() == required_length;
139 bool IsValidEmailAddress(const base::string16& text) {
140 // E-Mail pattern as defined by the WhatWG. (4.10.7.1.5 E-Mail state)
141 const base::string16 kEmailPattern = base::ASCIIToUTF16(
142 "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@"
143 "[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$");
144 return MatchesPattern(text, kEmailPattern);
147 bool IsValidState(const base::string16& text) {
148 return !state_names::GetAbbreviationForName(text).empty() ||
149 !state_names::GetNameForAbbreviation(text).empty();
152 bool IsValidZip(const base::string16& text) {
153 const base::string16 kZipPattern = base::ASCIIToUTF16("^\\d{5}(-\\d{4})?$");
154 return MatchesPattern(text, kZipPattern);
157 bool IsSSN(const base::string16& text) {
158 base::string16 number_string;
159 base::RemoveChars(text, kSSNSeparators, &number_string);
161 // A SSN is of the form AAA-GG-SSSS (A = area number, G = group number, S =
162 // serial number). The validation we do here is simply checking if the area,
163 // group, and serial numbers are valid.
165 // Historically, the area number was assigned per state, with the group number
166 // ascending in an alternating even/odd sequence. With that scheme it was
167 // possible to check for validity by referencing a table that had the highest
168 // group number assigned for a given area number. (This was something that
169 // Chromium never did though, because the "high group" values were constantly
172 // However, starting on 25 June 2011 the SSA began issuing SSNs randomly from
173 // all areas and groups. Group numbers and serial numbers of zero remain
174 // invalid, and areas 000, 666, and 900-999 remain invalid.
176 // References for current practices:
177 // http://www.socialsecurity.gov/employer/randomization.html
178 // http://www.socialsecurity.gov/employer/randomizationfaqs.html
180 // References for historic practices:
181 // http://www.socialsecurity.gov/history/ssn/geocard.html
182 // http://www.socialsecurity.gov/employer/stateweb.htm
183 // http://www.socialsecurity.gov/employer/ssnvhighgroup.htm
185 if (number_string.length() != 9 || !IsStringASCII(number_string))
189 if (!base::StringToInt(StringPiece16(number_string.begin(),
190 number_string.begin() + 3),
201 if (!base::StringToInt(StringPiece16(number_string.begin() + 3,
202 number_string.begin() + 5),
209 if (!base::StringToInt(StringPiece16(number_string.begin() + 5,
210 number_string.begin() + 9),
219 } // namespace autofill