Function isValidHex

  • 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, 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(isValidHex('#ff0000')); // true
    console.log(isValidHex('ff0000')); // true
    console.log(isValidHex('#FFF')); // true
    console.log(isValidHex('FFF')); // true
    console.log(isValidHex('#12345678')); // true
    console.log(isValidHex('12345678')); // true
    console.log(isValidHex('#12345')); // false
    console.log(isValidHex('#gggggg')); // false
    console.log(isValidHex(null)); // false
    console.log(isValidHex(123 as any)); // false

    Remarks

    The isValidHex function is used in various scenarios where hexadecimal color validation is needed, such as in functions like hexToBinary and hexToRgba. It ensures that only valid hexadecimal color codes are processed, preventing errors and unexpected behavior.