Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6349371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:36:10+00:00 2026-05-24T21:36:10+00:00

I was wondering how I would best validate URLs in Rails. I was thinking

  • 0

I was wondering how I would best validate URLs in Rails. I was thinking of using a regular expression, but am not sure if this is the best practice.

And, if I were to use a regex, could someone suggest one to me? I am still new to Regex.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:36:12+00:00Added an answer on May 24, 2026 at 9:36 pm

    Validating an URL is a tricky job. It’s also a very broad request.

    What do you want to do, exactly? Do you want to validate the format of the URL, the existence, or what? There are several possibilities, depending on what you want to do.

    A regular expression can validate the format of the URL. But even a complex regular expression cannot ensure you are dealing with a valid URL.

    For instance, if you take a simple regular expression, it will probably reject the following host

    http://invalid##host.com
    

    but it will allow

    http://invalid-host.foo
    

    that is a valid host, but not a valid domain if you consider the existing TLDs. Indeed, the solution would work if you want to validate the hostname, not the domain because the following one is a valid hostname

    http://host.foo
    

    as well the following one

    http://localhost
    

    Now, let me give you some solutions.

    If you want to validate a domain, then you need to forget about regular expressions. The best solution available at the moment is the Public Suffix List, a list maintained by Mozilla. I created a Ruby library to parse and validate domains against the Public Suffix List, and it’s called PublicSuffix.

    If you want to validate the format of an URI/URL, then you might want to use regular expressions. Instead of searching for one, use the built-in Ruby URI.parse method.

    require 'uri'
    
    def valid_url?(uri)
      uri = URI.parse(uri) && uri.host.present?
    rescue URI::InvalidURIError
      false
    end
    

    You can even decide to make it more restrictive. For instance, if you want the URL to be an HTTP/HTTPS URL, then you can make the validation more accurate.

    require 'uri'
    
    def valid_url?(url)
      uri = URI.parse(url)
      uri.is_a?(URI::HTTP) && uri.host.present?
    rescue URI::InvalidURIError
      false
    end
    

    Of course, there are tons of improvements you can apply to this method, including checking for a path or a scheme.

    Last but not least, you can also package this code into a validator:

    class HttpUrlValidator < ActiveModel::EachValidator
    
      def self.compliant?(value)
        uri = URI.parse(value)
        uri.is_a?(URI::HTTP) && uri.host.present?
      rescue URI::InvalidURIError
        false
      end
    
      def validate_each(record, attribute, value)
        unless value.present? && self.class.compliant?(value)
          record.errors.add(attribute, "is not a valid HTTP URL")
        end
      end
    
    end
    
    # in the model
    validates :example_attribute, http_url: true
    

    Note for newer URI versions(i.e 0.12.1)

    .present? / .blank? would be a more accurate way to validate hosts, instead of using uri.host.nil? or just if uri.host previously (i.e. URI v 0.11).

    Example for URI.parse("https:///394&quot;):

    • new URI version(0.12), host will return an empty string, and /394 becomes a path. #<URI::HTTPS https:///394&gt;
    • old URI version (0.11),host will return an empty string, and /394 becomes a path too. #<URI::HTTPS https:/394>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering what would be considered best practise for this. It's fairly inconsequential but
I was just wondering what the best practice would be in this scenario: I
I was wondering, what would be the best way to validate an integer. I'd
Wondering what the best / good way of doing this would be in jQuery.
I was wondering what would be the best way to accomplish something like this...
I was wondering what would be the best programming practice for jQuery. Most jquery
I am wondering what is the best way to validate multidimensional array using Zend_Validate
I was wondering what would be the best practice to deploy a maven packaged
I'm wondering what would be best to do. Right now I have running a
Hey im just wondering what would be the best collection to use when creating

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.