Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

URL Regex

DZone's Guide to

URL Regex

·
Free Resource
// description of your code here
A regular expression pattern that validates a URL string, either HTTP or HTTPS.


/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix


You can easily use it with validates_format_of in your Model...

For example, In a comment model, to check the :SiteURL on the comment creation:


class Comment < ActiveRecord::Base
    validates_format_of :SiteURL, :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix, :on => :create
end


Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}