There are different options here:
1. Use a dummy element
Use browser validation. Create a dummy HTML element, assign a color, and check if it is installed. This is by far the best option. This is not only easier, but also allows for direct compatibility.
function CheckValidColor(color) { var e = document.getElementById('divValidColor'); if (!e) { e = document.createElement('div'); e.id = 'divValidColor'; } e.style.borderColor = ''; e.style.borderColor = color; var tmpcolor = e.style.borderColor; if (tmpcolor.length == 0) { return false; } return true; }
This will return true for all colors accepted by the browser, even in cases that you may consider invalid.
2. Capturing regular expression numbers and checking in code
If you capture everything that looks like a number, you can check each parameter separately with IF clauses . This will allow you to provide better user feedback.
a) #hex:
^(#)((?:[A-Fa-f0-9]{3}){1,2})$
The hash is also fixed for consistency with the following expression. Demo
b) rgb (), rgba (), hsl () and hsla ():
^(rgb|hsl)(a?)[(]\s*([\d.]+\s*%?)\s*,\s*([\d.]+\s*%?)\s*,\s*([\d.]+\s*%?)\s*(?:,\s*([\d.]+)\s*)?[)]$
This will create captures for the function and each parameter. Demo
3. Confirm with a single monster regex:
This option is not recommended, mainly because it is rather difficult to read, it does not guarantee compliance with all use cases, and it will give you a headache if you try to debug it. The following regular expression checks the number of allowed parameters and ranges.
a) #hex: ^#(?:[A-Fa-f0-9]{3}){1,2}$ DEMO
b) rgb (): ^rgb[(](?:\s*0*(?:\d\d?(?:\.\d+)?(?:\s*%)?|\.\d+\s*%|100(?:\.0*)?\s*%|(?:1\d\d|2[0-4]\d|25[0-5])(?:\.\d+)?)\s*(?:,(?![)])|(?=[)]))){3}[)]$ DEMO
c) rgba (): ^^rgba[(](?:\s*0*(?:\d\d?(?:\.\d+)?(?:\s*%)?|\.\d+\s*%|100(?:\.0*)?\s*%|(?:1\d\d|2[0-4]\d|25[0-5])(?:\.\d+)?)\s*,){3}\s*0*(?:\.\d+|1(?:\.0*)?)\s*[)]$ DEMO
d) hsl (): ^hsl[(]\s*0*(?:[12]?\d{1,2}|3(?:[0-5]\d|60))\s*(?:\s*,\s*0*(?:\d\d?(?:\.\d+)?\s*%|\.\d+\s*%|100(?:\.0*)?\s*%)){2}\s*[)]$ DEMO
e) hsla (): ^hsla[(]\s*0*(?:[12]?\d{1,2}|3(?:[0-5]\d|60))\s*(?:\s*,\s*0*(?:\d\d?(?:\.\d+)?\s*%|\.\d+\s*%|100(?:\.0*)?\s*%)){2}\s*,\s*0*(?:\.\d+|1(?:\.0*)?)\s*[)]$ DEMO
That's it:
Using the above expressions, we can create this single-line key to check all valid color values:
^(?:#(?:[A-Fa-f0-9]{3}){1,2}|(?:rgb[(](?:\s*0*(?:\d\d?(?:\.\d+)?(?:\s*%)?|\.\d+\s*%|100(?:\.0*)?\s*%|(?:1\d\d|2[0-4]\d|25[0-5])(?:\.\d+)?)\s*(?:,(?![)])|(?=[)]))){3}|hsl[(]\s*0*(?:[12]?\d{1,2}|3(?:[0-5]\d|60))\s*(?:\s*,\s*0*(?:\d\d?(?:\.\d+)?\s*%|\.\d+\s*%|100(?:\.0*)?\s*%)){2}\s*|(?:rgba[(](?:\s*0*(?:\d\d?(?:\.\d+)?(?:\s*%)?|\.\d+\s*%|100(?:\.0*)?\s*%|(?:1\d\d|2[0-4]\d|25[0-5])(?:\.\d+)?)\s*,){3}|hsla[(]\s*0*(?:[12]?\d{1,2}|3(?:[0-5]\d|60))\s*(?:\s*,\s*0*(?:\d\d?(?:\.\d+)?\s*%|\.\d+\s*%|100(?:\.0*)?\s*%)){2}\s*,)\s*0*(?:\.\d+|1(?:\.0*)?)\s*)[)])$
Demo
Or regular expression enthusiasts can check out this huge regular expression with 148 color names. But I would never recommend using this template. Use the first option by creating a dummy element, which is the only way to cover all browser use cases.