Color Validator Util Function

May 17, 2023

Util Functions

Color Validator Util Function

Thuta Sann

Thuta Sann

In this section, Color validator function that will validate the color string will be written.


Share This Snippet To :

This util function will check if the input string is the valid color string or not. Note: for client side only, not work in NodeJS.
function isValidColor(colorString : string) : boolean { let otpNode = new Option() otpNode.style.color = colorString return !!otpNode.style.color } // Usage isValidColor('purple') // true isValidColor('burlywood') // true isValidColor('lightsalmon') // true isValidColor('rgb(255, 255, 255)') // true isValidColor('rgba(255, 255, 255, .5)') // true isValidColor('#ccc') // true isValidColor('hsl(100, 0%, 18%)') // true isValidColor('not-a-color-name') // false isValidColor('dark gray') // false. Should be 'darkgray' isValidColor('rgb(255, 255, 255, 1, 1)') // false isValidColor('#ccczzz') // false

Cookie

I baked some cookies that you have to accept, if you want to enjoy my portfolio.
In order to gather information and make improvements, I should use some third-party cookies too, Can I?