Skip to content

Instantly share code, notes, and snippets.

@sagar-ranglani
Created July 16, 2013 02:58
Show Gist options
  • Save sagar-ranglani/6005408 to your computer and use it in GitHub Desktop.
Save sagar-ranglani/6005408 to your computer and use it in GitHub Desktop.
require 'uri'
#Ruby Method for URI validation
def valid?(url)
uri = URI.parse(url)
rescue URI::InvalidURIError
false
end
#Ruby Method for URI validation with HTTP and HTTPS
def valid?(url)
uri = URI.parse(url)
uri.kind_of?(URI::HTTP) # This will allow both HTTP and HTTPS
rescue URI::InvalidURIError
false
end
# Custom Validator for Rails
class UriValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
uri = URI.parse(url)
rescue URI::InvalidURIError
false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment