Sha256: 6ba8a1ebbd6c5e7a99917e7cdd397eb295182c2d4a598e5e7b6bd8b7ddae7da4
Contents?: true
Size: 999 Bytes
Versions: 5
Compression:
Stored size: 999 Bytes
Contents
module ActiveModel module Validations class PhoneValidator < EachValidator def validate_each(record, attribute, value) @value = value @formats = PhoneValidator.known_formats[options[:country]] || PhoneValidator.known_formats[:us] unless matches_any? record.errors.add(attribute) end end def self.known_formats @@known_formats ||= {:us => ["###-###-####", "##########", "###.###.####", "### ### ####", "(###) ###-####"], :brazil => ["## ####-####", "(##) ####-####", "##########"], :france => ["## ## ## ## ##"], :uk => ["#### ### ####"]} end def matches_any? false if @formats.nil? or not @formats.respond_to?(:detect) @formats.detect { |format| @value.match(PhoneValidator.regexp_from format) } end private def self.regexp_from(format) Regexp.new "^"+(Regexp.escape format).gsub('\#','\d')+"$" end end end end
Version data entries
5 entries across 5 versions & 1 rubygems