Skip to content

Instantly share code, notes, and snippets.

@michaelcarter
Created February 19, 2013 11:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelcarter/4985109 to your computer and use it in GitHub Desktop.
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...
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