Function isHexColor

  • Validates whether a given string is a valid hexadecimal color code. The function ensures that the input string matches the format of either a 3, 4, 6, or 8-digit hexadecimal number, optionally prefixed with a #.

    Parameters

    • hex: string

      The input string to be validated as a hexadecimal color code.

    Returns boolean

    true if the input string is a valid hexadecimal color code, otherwise false.

    Example

    console.log(isHexColor('#ff0000')); // true
    console.log(isHexColor('#FFF')); // true
    console.log(isHexColor('#12345678')); // true
    console.log(isHexColor('ff0000')); // true
    console.log(isHexColor('#12345')); // false
    console.log(isHexColor('#gggggg')); // false
    console.log(isHexColor(null)); // false
    console.log(isHexColor(123 as any)); // false