Enlightment via blog-kata

URL Validation in Rails

leave a comment »

URL validation in Rails. The secret is a little-known method of the URI class, regexp. It lets you generate a regular expression for matching URLs based on an array of accepted protocols. What’s even better, it can be plugged directly into Rails’s validates_format_of. It’s this easy:

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

This will match anything that URI.parse will recognize, meaning that it’s a pretty accurate and powerful URL matcher.

via Intridea – Company Blog.


Written by zhesto

20-Feb-2009 at 12:23 UTC

Posted in Microblog, Texts

Tagged with ,

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: