Skip to content

Instantly share code, notes, and snippets.

@jasonneylon
Created December 29, 2012 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonneylon/4407858 to your computer and use it in GitHub Desktop.
Save jasonneylon/4407858 to your computer and use it in GitHub Desktop.
class PostcodeValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless value =~ /^([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
record.errors[attribute] << (options[:message] || "invalid postcode")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment