How to check if a string is a valid hex color representation?

后端 未结 7 532
迷失自我
迷失自我 2020-11-28 03:47

For example:

AA33FF = valid hex color

Z34FF9 = invalid hex color (has Z in it)

AA33FF11 = invalid hex color (h

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 04:06

    function validColor(color){
      var $div = $("
    "); $div.css("border", "1px solid "+color); return ($div.css("border-color")!="") }

    https://gist.github.com/dustinpoissant/22ce25c9e536bb2c5a2a363601ba261c

    Note: This requires jQuery

    This works for ALL color types not just hex values. It also does not append unnecessary elements to the DOM tree.

提交回复
热议问题