Skip to content

Instantly share code, notes, and snippets.

View Disha-Shah's full-sized avatar

Disha Shah Disha-Shah

  • Ahmedabad, India
View GitHub Profile
@mudge
mudge / gist:163332
Created August 6, 2009 14:19
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