Validation for URL/Domain using Regex? (Rails)

后端 未结 13 1759
野的像风
野的像风 2020-11-29 21:10

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.spli         


        
13条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 21:52

    ^(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...

提交回复
热议问题