1class PostcodeValidator < ActiveModel::EachValidator
 
2  STANDARD = "([A-Z]{1,2}[0-9][0-9A-Z]?[0-9][A-BD-HJLNP-UW-Z]{2})"
 
3  BFPO     = "(BFPO?(C\/O)?[0-9]{1,4})"
 
4  GIROBANK = "(GIR0AA)"
 
5  PATTERN  = /\A(#{STANDARD}|#{BFPO}|#{GIROBANK})\Z/i
 
6
  • Complexity 2 » saikuro
7  def validate_each(record, attribute, value)
 
8    unless value.to_s =~ PATTERN
 
 9      record.errors.add(attribute, (options[:message] || :invalid))
 
10    end
 
11  end
 
12end