I want to parse the input and check whether the entered colored format is in either RGBA or HEX format and also give user space for entering color names of regular colors with a switch case.
I found a regex for hex colors and regular color names but was unable to find one for RGBA.
Javascript
function verifycolor(colorcode) {
var regColorcode = /^(#)?([0-9a-fA-F]{3})([0-9a-fA-F]{3})?$/;
if(regColorcode.test(colorcode) == false)
document.getElementById("status").innerHTML = "Color is not yet valid.";
else if(regColorcode.test(colorcode) == true)
document.getElementById("status").innerHTML = "You have entered a valid color code";
}
jsfiddle didn’t work for me with this code. Dunno the reason. So it’s here.
Can anyone help me out to do the same for RGBA colors. It would be great if the regex can parse it into red, green, blue and alpha.
Some search gave me this RGB color parser:
It does exactly what you want (even can output the color channels). And of course it can be used for color verification.
From the description:
DEMO: http://www.phpied.com/files/rgbcolor/rgbcolor.html