Created
August 6, 2009 14:19
-
-
Save mudge/163332 to your computer and use it in GitHub Desktop.
A Ruby method to validate UK postcodes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Validate a UK postcode using a modified version of the official | |
# regular expression provided by | |
# http://www.govtalk.gov.uk/gdsc/schemaHtml/bs7666-v2-0-xsd-PostCodeType.htm | |
# | |
# @param [String] postcode the postcode to validate | |
# @return [Boolean] true if the postcode is valid, false if not | |
def is_valid_postcode?(postcode) | |
!!(postcode =~ /^\s*((GIR\s*0AA)|((([A-PR-UWYZ][0-9]{1,2})|(([A-PR-UWYZ][A-HK-Y][0-9]{1,2})|(([A-PR-UWYZ][0-9][A-HJKSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY]))))\s*[0-9][ABD-HJLNP-UW-Z]{2}))\s*$/i) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment