be090129 / substruct

Automatically exported from code.google.com/p/substruct
0 stars 0 forks source link

US states validation #142

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
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

Original issue reported on code.google.com by rogerpack2005 on 17 Oct 2008 at 6:29

GoogleCodeExporter commented 8 years ago
note also that the current validation in order_address for 'city, state if 
country ==  
USA' fails if their USA is a different id than 1 [which it frequently is, at 
least 
for my setups it has been].

Original comment by rogerpack2005 on 8 Dec 2008 at 9:43

GoogleCodeExporter commented 8 years ago
this validates [mostly] a zip, too.  Could use a zip -> state double check, but 
this 
is an improvement

Original comment by rogerpack2005 on 16 Dec 2008 at 8:16

GoogleCodeExporter commented 8 years ago
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

Original comment by rogerpack2005 on 24 Jan 2009 at 10:19

Attachments:

GoogleCodeExporter commented 8 years ago
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.

Original comment by subim...@gmail.com on 25 Jan 2009 at 7:16

GoogleCodeExporter commented 8 years ago
done

Original comment by rogerpack2005 on 12 Sep 2009 at 7:54

GoogleCodeExporter commented 8 years ago
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}

Original comment by dlredde...@gmail.com on 12 Dec 2009 at 2:58

GoogleCodeExporter commented 8 years ago

Original comment by subim...@gmail.com on 14 Sep 2010 at 6:27