OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/core/browser/validation.h" | 5 #include "components/autofill/core/browser/validation.h" |
6 | 6 |
7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
8 #include "base/strings/string_piece.h" | 8 #include "base/strings/string_piece.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
12 #include "components/autofill/core/browser/autofill_regexes.h" | 12 #include "components/autofill/core/browser/autofill_regexes.h" |
13 #include "components/autofill/core/browser/credit_card.h" | 13 #include "components/autofill/core/browser/credit_card.h" |
14 #include "components/autofill/core/browser/state_names.h" | 14 #include "components/autofill/core/browser/state_names.h" |
15 | 15 |
16 using base::StringPiece16; | |
17 | |
18 namespace { | |
19 | |
20 // The separator characters for SSNs. | |
21 const base::char16 kSSNSeparators[] = {' ', '-', 0}; | |
22 | |
23 } // namespace | |
24 | 16 |
25 namespace autofill { | 17 namespace autofill { |
26 | 18 |
27 bool IsValidCreditCardExpirationDate(const base::string16& year, | 19 bool IsValidCreditCardExpirationDate(const base::string16& year, |
28 const base::string16& month, | 20 const base::string16& month, |
29 const base::Time& now) { | 21 const base::Time& now) { |
30 base::string16 year_cleaned, month_cleaned; | 22 base::string16 year_cleaned, month_cleaned; |
31 base::TrimWhitespace(year, base::TRIM_ALL, &year_cleaned); | 23 base::TrimWhitespace(year, base::TRIM_ALL, &year_cleaned); |
32 base::TrimWhitespace(month, base::TRIM_ALL, &month_cleaned); | 24 base::TrimWhitespace(month, base::TRIM_ALL, &month_cleaned); |
33 if (year_cleaned.length() != 4) | 25 if (year_cleaned.length() != 4) |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 !state_names::GetNameForAbbreviation(text).empty(); | 141 !state_names::GetNameForAbbreviation(text).empty(); |
150 } | 142 } |
151 | 143 |
152 bool IsValidZip(const base::string16& text) { | 144 bool IsValidZip(const base::string16& text) { |
153 const base::string16 kZipPattern = base::ASCIIToUTF16("^\\d{5}(-\\d{4})?$"); | 145 const base::string16 kZipPattern = base::ASCIIToUTF16("^\\d{5}(-\\d{4})?$"); |
154 return MatchesPattern(text, kZipPattern); | 146 return MatchesPattern(text, kZipPattern); |
155 } | 147 } |
156 | 148 |
157 bool IsSSN(const base::string16& text) { | 149 bool IsSSN(const base::string16& text) { |
158 base::string16 number_string; | 150 base::string16 number_string; |
159 base::RemoveChars(text, kSSNSeparators, &number_string); | 151 base::RemoveChars(text, base::ASCIIToUTF16("- "), &number_string); |
160 | 152 |
161 // A SSN is of the form AAA-GG-SSSS (A = area number, G = group number, S = | 153 // 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, | 154 // serial number). The validation we do here is simply checking if the area, |
163 // group, and serial numbers are valid. | 155 // group, and serial numbers are valid. |
164 // | 156 // |
165 // Historically, the area number was assigned per state, with the group number | 157 // 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 | 158 // 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 | 159 // 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 | 160 // group number assigned for a given area number. (This was something that |
169 // Chromium never did though, because the "high group" values were constantly | 161 // Chromium never did though, because the "high group" values were constantly |
170 // changing.) | 162 // changing.) |
171 // | 163 // |
172 // However, starting on 25 June 2011 the SSA began issuing SSNs randomly from | 164 // 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 | 165 // all areas and groups. Group numbers and serial numbers of zero remain |
174 // invalid, and areas 000, 666, and 900-999 remain invalid. | 166 // invalid, and areas 000, 666, and 900-999 remain invalid. |
175 // | 167 // |
176 // References for current practices: | 168 // References for current practices: |
177 // http://www.socialsecurity.gov/employer/randomization.html | 169 // http://www.socialsecurity.gov/employer/randomization.html |
178 // http://www.socialsecurity.gov/employer/randomizationfaqs.html | 170 // http://www.socialsecurity.gov/employer/randomizationfaqs.html |
179 // | 171 // |
180 // References for historic practices: | 172 // References for historic practices: |
181 // http://www.socialsecurity.gov/history/ssn/geocard.html | 173 // http://www.socialsecurity.gov/history/ssn/geocard.html |
182 // http://www.socialsecurity.gov/employer/stateweb.htm | 174 // http://www.socialsecurity.gov/employer/stateweb.htm |
183 // http://www.socialsecurity.gov/employer/ssnvhighgroup.htm | 175 // http://www.socialsecurity.gov/employer/ssnvhighgroup.htm |
184 | 176 |
185 if (number_string.length() != 9 || !base::IsStringASCII(number_string)) | 177 if (number_string.length() != 9 || !base::IsStringASCII(number_string)) |
186 return false; | 178 return false; |
187 | 179 |
188 int area; | 180 int area; |
189 if (!base::StringToInt(StringPiece16(number_string.begin(), | 181 if (!base::StringToInt(base::StringPiece16(number_string.begin(), |
190 number_string.begin() + 3), | 182 number_string.begin() + 3), |
191 &area)) { | 183 &area)) { |
192 return false; | 184 return false; |
193 } | 185 } |
194 if (area < 1 || | 186 if (area < 1 || |
195 area == 666 || | 187 area == 666 || |
196 area >= 900) { | 188 area >= 900) { |
197 return false; | 189 return false; |
198 } | 190 } |
199 | 191 |
200 int group; | 192 int group; |
201 if (!base::StringToInt(StringPiece16(number_string.begin() + 3, | 193 if (!base::StringToInt(base::StringPiece16(number_string.begin() + 3, |
202 number_string.begin() + 5), | 194 number_string.begin() + 5), |
203 &group) | 195 &group) |
204 || group == 0) { | 196 || group == 0) { |
205 return false; | 197 return false; |
206 } | 198 } |
207 | 199 |
208 int serial; | 200 int serial; |
209 if (!base::StringToInt(StringPiece16(number_string.begin() + 5, | 201 if (!base::StringToInt(base::StringPiece16(number_string.begin() + 5, |
210 number_string.begin() + 9), | 202 number_string.begin() + 9), |
211 &serial) | 203 &serial) |
212 || serial == 0) { | 204 || serial == 0) { |
213 return false; | 205 return false; |
214 } | 206 } |
215 | 207 |
216 return true; | 208 return true; |
217 } | 209 } |
218 | 210 |
219 } // namespace autofill | 211 } // namespace autofill |
OLD | NEW |