Class: OmgValidator::Validators::UrlValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/omg_validator/validators/url_validator.rb

Overview

Checks whether input is a valid URL Supports the protocols: http, https and ftp

Matches:

http://www.example.com, www.example.com, example.com, sub.example.com

Does not match:

@asd.com, domain.c, domain.asd., [email protected]

Examples:

validates :url, url: true

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/omg_validator/validators/url_validator.rb', line 15

def validate_each(record, attribute, value)
  return nil if value.blank?
  reg = /^((https?):\/\/)?([a-z\d]+([\-\.][a-z\d]+)*\.[a-z]{2,6})((:(\d{1,5}))?(\/.*)?)?$/
  unless reg.match(value)
    record.errors[attribute] = "must be a valid url"
  end
end