Sha256: 022c4f4736490d6295412de567b39386f45f01385e744841bffbfcf0552b22ac
Contents?: true
Size: 973 Bytes
Versions: 1
Compression:
Stored size: 973 Bytes
Contents
class TelephoneValidator < ActiveModel::EachValidator def validate_each(object, attribute, value) formatted_value = value.gsub(/[\s\.\+\(\)-]*/, "") is_number = !(formatted_value =~ /^[-+]?[0-9]+$/).nil? unless is_number && validate_by_region(options[:region], formatted_value) object.errors[attribute] << (options[:message] || "is not formatted properly") end end def validate_by_region(region, formatted_value) if region == :north_america valid_eleven_digit?(formatted_value) || valid_ten_digit?(formatted_value) || valid_seven_digit?(formatted_value) end end def valid_seven_digit?(input) input.length == 7 && input[0] != "0" && input[0] != "1" end def valid_ten_digit?(input) input.length == 10 && input[0] != "0" && input[0] != "1" && valid_seven_digit?(input.slice(-7, 7)) end def valid_eleven_digit?(input) input.length == 11 && input[0] == "1" && valid_ten_digit?(input.slice(-10, 10)) end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
drexed-validators-0.0.11 | app/validators/telephone_validator.rb |