views:

450

answers:

1

i'm using the following to verify if a URL is formatted validly:

validates_format_of :website, :with => URI::regexp(%w(http https))

however, it doesn't work when the url doesn't start with http:// or https://. Is there some similar way to validate URLs with URI::regexp (or URI) and make it include valid URLs that don't start with http://? (For example, www.google.com is valid, as is http://www.google.com)

+1  A: 

This recent post on daring fireball provides a robust regex: http://daringfireball.net/2009/11/liberal%5Fregex%5Ffor%5Fmatching%5Furls

From my tests URL::regexp is to loose in its definition of a uri (though it does require http...)

You can use a virtual attribute or before_save filter to append a http:// to your URLs if necessary.

nutcracker