Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation for URL/Domain using Regex? (Rails)

I am trying to create a validation that checks to make sure a domain/url is valid for example "test.com"

def valid_domain_name?
  domain_name = domain.split(".")
  name = /(?:[A-Z0-9\-])+/.match(domain_name[0]).nil?
  tld = /(?:[A-Z]{2}|aero|ag|asia|at|be|biz|ca|cc|cn|com|de|edu|eu|fm|gov|gs|jobs|jp|in|info|me|mil|mobi|museum|ms|name|net|nu|nz|org|tc|tw|tv|uk|us|vg|ws)/.match(domain_name[1]).nil?
  if name == false or tld == false
    errors.add(:domain_name, 'Invalid domain name. Please only use names with letters (A-Z) and numbers (0-9).')
  end
end

This is what I have so far but it doesn't work. It lets bad URLs through without failing.

I don't know regex very well.

like image 331
kush Avatar asked Jul 14 '09 21:07

kush


3 Answers

Stumbled on this:

validates_format_of :domain_name, :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix

FYI: Rubular is a fantastic resource for testing your Ruby regular expressions

like image 152
Tate Johnson Avatar answered Nov 10 '22 23:11

Tate Johnson


@Tate's answer is good for a full URL, but if you want to validate a domain column, you don't want to allow the extra URL bits his regex allows (e.g. you definitely don't want to allow a URL with a path to a file).

So I removed the protocol, port, file path, and query string parts of the regex, resulting in this:

^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}$


Check out the same test cases for both versions.

  • Original (allows domains or full URLs): http://rubular.com/r/qGInC06jcz
  • Modified (allows only domains): http://rubular.com/r/yP6dHFEhrl
like image 37
Brian Ray Avatar answered Nov 11 '22 01:11

Brian Ray


^(http|https):\/\/|[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}(:[0-9]{1,5})?(\/.*)?$/ix
  • example.com
  • sub.example.com
  • sub.domain.my-example.com
  • example.com/?stuff=true
  • example.com:5000/?stuff=true
  • sub.domain.my-example.com/path/to/file/hello.html
  • hello.museum
  • http://railsgirls.com

http://rubular.com/r/cdkLxAkTbk

Added optional http:// or https://

The longest TLD is .museum, which has 6 characters...

like image 16
Jez Avatar answered Nov 11 '22 01:11

Jez