internetee-registry/app/validators/hostname_validator.rb
2014-08-05 16:08:31 +03:00

13 lines
420 B
Ruby

class HostnameValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if !self.class.validate_format(value)
record.errors.add(attribute, (options[:message] || :invalid))
end
end
class << self
def validate_format(value)
!!(value =~ /\A(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])\z/)
end
end
end