| Issue 142: | US states validation | |
| 2 people starred this issue and may be notified of changes. | Back to list |
What steps will reproduce the problem?
1. checkout, misspell your state
2.
3.
What is the expected output? What do you see instead?
should err, since this prohibits users from correctly calculating sales
tax, if they do.
Here's a rough hack at it.
This goes in OrderAddress.rb
US_STATES =
%\AK
AZ
CT
FL
HI
IL
KY
MD
MN
MT
NE
NM
OH
PA
SC
TX
VI
WI
AL
CA
DC
GA
IA
IN
LA
ME
MO
NC
NH
NV
OK
PR
SD
UT
VT
WV
AR
CO
DE
GU
ID
KS
MA
MI
MS
ND
NJ
NY
OR
RI
TN
VA
WA
WY\.split("\n").map{|state| state.strip}
US = Country.find :first, :conditions => {:fedex_code => 'US'}
# Makes sure validation address doesn't allow PO Box or variants
def validate
invalid_strings = ['PO BOX', 'P.O. BOX', 'P.O BOX', 'PO. BOX',
'POBOX', 'P.OBOX', 'P.O.BOX', 'PO.BOX', 'P.BOX',
'PBOX', 'AFO', 'A.F.O.', 'APO', 'A.P.O.']
cap_address = self.address.upcase()
invalid_strings.each do |string|
if cap_address.include?(string) then
#errors.add(:address, "Sorry, we don't ship to P.O. boxes")
end
end
unless zip.blank?
unless zip =~ /\d{5}/ or zip =~/... .../
errors.add(:zip, "Please enter a valid zip.")
end
end
if country == US
self.state = self.state.upcase
errors.add(:state, "Please use a US state abbreviation") unless
self.state.in? US_STATES
end
end
Dec 8, 2008
#1
rogerpack2005
Dec 16, 2008
this validates [mostly] a zip, too. Could use a zip -> state double check, but this is an improvement
Jan 24, 2009
Here's an updated one which also takes into account that the US country might not be 1 [sometimes it isn't for me, for some reason it's like id 41235562 some large number]. This patch also allows for things like australia to have a 3 digit zip [which is apparently necessary, according to a client]. -=r
Jan 24, 2009
Applied patch - it breaks the tests. Please ensure vendor/plugins/substruct/tests/unit/order_address_test.rb passes. Also, please add code to express this new code works as it should. Thanks.
Status:
Started
Sep 12, 2009
done
Dec 12, 2009
Updated list of postal abbreviations
US_STATES =
%\AA
AE
AK
AL
AP
AR
AS
AZ
CA
CO
CT
DC
DE
FL
FM
GA
GU
HI
IA
ID
IL
IN
KS
KY
LA
MA
MD
ME
MH
MI
MN
MO
MP
MS
MT
NC
ND
NE
NH
NJ
NM
NV
NY
OH
OK
OR
PA
PR
PW
RI
SC
SD
TN
TX
UT
VA
VI
VT
WA
WI
WV
WY\.split("\n").map{|state| state.strip}
Sep 13, 2010
(No comment was entered for this change.)
Status:
Fixed
|