Class: TaiwanValidator::PhoneNumberValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/taiwan_validator/phone_number_validator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.valid?(phone_number) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/taiwan_validator/phone_number_validator.rb', line 3

def valid?(phone_number)
  phone_number.gsub(/[\+\-\s]/, "") =~ /\A(886|0)[2-9]([0-9]{6,8})\z/
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



8
9
10
11
12
# File 'lib/taiwan_validator/phone_number_validator.rb', line 8

def validate_each(record, attribute, value)
  unless self.class.valid?(value)
    record.errors.add(attribute, options[:message] || :invalid)
  end
end