Created
February 19, 2013 11:42
-
-
Save michaelcarter/4985109 to your computer and use it in GitHub Desktop.
Custom validator for CSS hex color codes. I use the American spelling of colour as that's how it appears in CSS...
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
class CssHexColorValidator < ActiveModel::EachValidator | |
def validate_each(object, attribute, value) | |
unless value =~ /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/i | |
object.errors[attribute] << (options[:message] || "must be a valid CSS hex color code") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment