How to validate url/urls format within your models in Rails

How to validate url/urls format within your models in Rails

validates_format_of :url, :with => /\A(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w\.-]*)*\/?\Z/i

Notice that I’m using “A” and “Z” because if you use ^ and $ you will see this warning security from Rails validators.

Valid ones:
'www.crowdint.com'
'crowdint.com'
'http://crowdint.com'
'http://www.crowdint.com'

Invalid ones:
'http://www.crowdint. com'
'http://fake'
'http:fake'
No Comments

Post A Comment