title: Ruby on Rails URL Validation (regex)
body: <p>I'm trying to use a regular expression to validate the format of a URL in my Rails model. I've tested the regex in Rubular with the URL <a href="http://trentscott.com" rel="nofollow noreferrer">http://trentscott.com</a> and it matched.</p>
  
  <p>Any idea why it fails validation when I test it in my Rails app (it says "name is invalid").</p>
  
  <p>Code:</p>
  
  <pre><code>  url_regex = /^((http|https):\/\/)?[a-z0-9]+([-.]{1}[a-z0-9]+).[a-z]{2,5}(:[0-9]{1,5})?(\/.)?$/ix
  
    validates :serial, :presence =&gt; true
    validates :name, :presence =&gt; true,
                     :format    =&gt; {  :with =&gt; url_regex  }
  </code></pre>