Skip to content

Instantly share code, notes, and snippets.

@jasonneylon
Created December 29, 2012 16:27
Show Gist options
  • Save jasonneylon/4407872 to your computer and use it in GitHub Desktop.
Save jasonneylon/4407872 to your computer and use it in GitHub Desktop.
Uk postcode validation with rails/activerecord
class Address < ActiveRecord::Base
attr_accessible :postcode
validates_format_of :postcode, :with => /^([A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW])\s?[0-9][ABD-HJLNP-UW-Z]{2}|(GIR\ 0AA)|(SAN\ TA1)|(BFPO\ (C\/O\ )?[0-9]{1,4})|((ASCN|BBND|[BFS]IQQ|PCRN|STHL|TDCU|TKCA)\ 1ZZ))$$/i, :message => "invalid postcode"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment