Skip to content

Instantly share code, notes, and snippets.

@mudge
Created August 6, 2009 14:19
Show Gist options
  • Save mudge/163332 to your computer and use it in GitHub Desktop.
Save mudge/163332 to your computer and use it in GitHub Desktop.
A Ruby method to validate UK postcodes.
# 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