class AddressValidator < ActiveModel::EachValidator # "(?(^\d{5}|^\d{3})?)(?\D+[縣市])(?\D+?(市區|鎮區|鎮市|[鄉鎮市區]))(?.+)" class << self def valid?(address) /(\A\d{3,5})?\s?(\D+[縣市])(\D+[鄉鎮市區])?(.+)/.match(address) end end def validate_each(record, attribute, value) unless self.class.valid?(value) record.errors.add(attribute, options[:message] || :invalid) end end end