例如:
AA33FF
=有效的十六进制颜色
Z34FF9
=无效的十六进制颜色(其中包含Z)
AA33FF11
=无效的十六进制颜色(有额外字符)
答案 0 :(得分:227)
var isOk = /^#[0-9A-F]{6}$/i.test('#aabbcc')
详细说明:
^
匹配开头
#
哈希
[a-f0-9]
来自a-f和0-9的任何来信
{6}
前一组恰好出现6次
$
匹配结束
i
忽略大小写
更高级:
var isOk = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test('#ac3') // for #f00 (Thanks Smamatti)
答案 1 :(得分:27)
function isHexaColor(sNum){
return (typeof sNum === "string") && sNum.length === 6
&& ! isNaN( parseInt(sNum, 16) );
}
isHexaColor("AA33FF") => true
isHexaColor("Z34FF9") => false
isHexaColor("AA33FF11") => false
编辑:请参阅下面@SalvadorDali的评论,在某些情况下会出现误报。而是使用另一种解决方案。
答案 2 :(得分:9)
这可能是一个复杂的问题。经过几次尝试,我想出了一个相当干净的解决方案。让browswer为你做的工作。
步骤1:创建一个border-style设置为none的div。 div可以在屏幕外定位,也可以是页面上不使用边框的任何div。
步骤2:将边框颜色设置为空字符串。代码可能如下所示:
e=document.getElementbyId('mydiv');
e.style.borderColor="";
步骤3:将边框颜色设置为您不确定的颜色。
e.style.borderColor=testcol;
步骤4:检查颜色是否实际发生了变化。如果testcol无效,则不会发生任何变化。
col2=e.style.borderColor;
if(col2.length==0) {alert("Bad Color!");}
步骤5:通过将颜色设置回空字符串来自行清理。
e.style.borderColor="";
The Div:
<div id="mydiv" style="border-style:none; position:absolute; left:-9999px; top:-9999px;"></div>
现在的JavaScript函数:
function GoodColor(color)
{
var color2="";
var result=true;
var e=document.getElementById('mydiv');
e.style.borderColor="";
e.style.borderColor=color;
color2=e.style.borderColor;
if (color2.length==0){result=false;}
e.style.borderColor="";
return result;
}
在这种情况下,函数返回问题的真/假答案,另一个选项是让它返回有效的颜色值。您的原始颜色值,borderColor中的值或空字符串代替无效颜色。
答案 3 :(得分:2)
function validColor(color){
var $div = $("<div>");
$div.css("border", "1px solid "+color);
return ($div.css("border-color")!="")
}
https://gist.github.com/dustinpoissant/22ce25c9e536bb2c5a2a363601ba261c
注意:这需要jQuery
这适用于所有颜色类型,而不仅仅是十六进制值。它也不将不必要的元素附加到DOM树。
答案 4 :(得分:1)
如果您需要一个函数来告诉您颜色是否有效,您可能会给它一些有用的东西 - 该颜色的计算值 - 并且当它不是有效颜色时返回null。这是我对兼容(Chrome54&amp; MSIE11)功能的尝试,以获得任何格式的“颜色”的RGBA值 - 无论是'绿色',还是'#FFF',还是'#89abcd',或' rgb(0,0,128)'或'rgba(0,128,255,0.5)'。
/* getRGBA:
Get the RGBA values of a color.
If input is not a color, returns NULL, else returns an array of 4 values:
red (0-255), green (0-255), blue (0-255), alpha (0-1)
*/
function getRGBA(value) {
// get/create a 0 pixel element at the end of the document, to use to test properties against the client browser
var e = document.getElementById('test_style_element');
if (e == null) {
e = document.createElement('span');
e.id = 'test_style_element';
e.style.width = 0;
e.style.height = 0;
e.style.borderWidth = 0;
document.body.appendChild(e);
}
// use the browser to get the computed value of the input
e.style.borderColor = '';
e.style.borderColor = value;
if (e.style.borderColor == '') return null;
var computedStyle = window.getComputedStyle(e);
var c
if (typeof computedStyle.borderBottomColor != 'undefined') {
// as always, MSIE has to make life difficult
c = window.getComputedStyle(e).borderBottomColor;
} else {
c = window.getComputedStyle(e).borderColor;
}
var numbersAndCommas = c.replace(new RegExp('[^0-9.,]+','g'),'');
var values = numbersAndCommas.split(',');
for (var i = 0; i < values.length; i++)
values[i] = Number(values[i]);
if (values.length == 3) values.push(1);
return values;
}
答案 5 :(得分:1)
如果您尝试在HTML中使用它请尝试直接使用此模式:
pattern="^#+([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$"
喜欢
<input id="hex" type="text" pattern="^#+([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$" />
它将提供与所请求格式匹配的验证。
答案 6 :(得分:0)
添加长度检查以确保您不会出现误报
function isValidHex(testNum){
let validHex = false;
let numLength = testNum.length;
let parsedNum = parseInt(testNum, 16);
if(!isNan(parsedNum) && parsedNum.length===numLength){
validHex = true;
}
return validHex;
}