If a color string is a number, then we can use a RegExp
to check if it's a valid CSS color. But what if it's a word?
I have code that generates controls dynamically from colors arguments. But instead of color, there could be null
, ""
, or random words. Is there a way to check color name with Javascript?
Update: Thank you much for the great answer! :)
My final version is below (added a toLowerCase()
check because the color could be "Green"
, "Red"
, etc.).
function isValidColor(strColor) {
var s = new Option().style;
s.color = strColor;
// return 'false' if color wasn't assigned
return s.color == strColor.toLowerCase();
}