Created
July 16, 2013 02:58
-
-
Save sagar-ranglani/6005408 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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