Math & Text
isValidHex
$isValidHex
The function $isValidHex[value] checks that a string is made only of hexadecimal digits (0-9, a-f, A-F), with an optional single # at the start. It does not check the length.
Syntax
$isValidHex[value]
Parameters
| Parameter | Description |
|---|---|
value |
The string to test, with or without the # prefix. Required, exactly one argument. |
Return Value
- Type: Boolean
trueif the string is one or more hexadecimal digits, optionally preceded by one#.falseif the string contains any other character (including spaces), is empty, or is just#.
Behavior
- Accepts
#RRGGBBandRRGGBB, but also any other length:#FFFand12aretrue. - Letters are case-insensitive (A-F or a-f).
- Spaces are not trimmed:
$isValidHex[ ff]isfalse. - A
0xprefix is not accepted (0xffisfalse). - If a 6-digit color is required, check the length separately, for example with
$charCount.
Examples
Validation before use
$var[couleur;$message[1]]
$if[$isValidHex[$var[couleur]]==true]
$addField[Color;$var[couleur];yes]
$color[$var[couleur]]
$sendMessage[✅ Embed with the color $var[couleur].]
$else
$sendMessage[❌ Invalid color. Expected format: #RRGGBB]
$endif
Colored role command
$var[couleur;$message[1]]
$if[$isValidHex[$var[couleur]]==true]
$modifyRole[$roleID[Color];color;$var[couleur]]
$sendMessage[🎨 The color of the role was changed to $var[couleur]!]
$else
$sendMessage[❌ Invalid format. Example: !color #FF5733]
$endif
Interactive palette
$var[hex;$message[1]]
$if[$isValidHex[$var[hex]]==true]
$title[🎨 Color Preview]
$description[**Hex:** $var[hex]]
$color[$var[hex]]
$addTimestamp[]
$else
$sendMessage[❌ Invalid hex format. Usage: !color #5865F2]
$endif
Notes
$isValidHex[#FF0000]returnstrue.$isValidHex[ff0000]returnstrue.$isValidHex[#FFF]returnstrue(the length is not checked).$isValidHex[#GG0000]returnsfalse(G is not hex).$isValidHex[](empty) returnsfalse.
Try it
Paste an example into the playground to preview its message (functions that act on Discord are not run), or build the same thing with blocks.