Javascript - check if string is valid CSS color?

前端 未结 6 1331
一生所求
一生所求 2021-01-01 23:25

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 contro

6条回答
  •  甜味超标
    2021-01-02 00:01

    The input is often incorrect with the output. If you check the length of the values, you get either an empty string or a value in the CSS2 tree

    function isColor(string) {
        var s = new Option().style;
        s.color = string;
        return s.color.length > 0;
    }
    

提交回复
热议问题