Home > Microblog, Texts > URL Validation in Rails

URL Validation in Rails

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))
end

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

via Intridea – Company Blog.

Categories: Microblog, Texts Tags: ,
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.