HTML5 Color Input Value in PY
Validate the value of an HTML5 `<input type="color">` — exactly 6 hex digits with leading hash.
Try it in the PY tester →Pattern
regexPY
^#[0-9a-fA-F]{6}$Python (re) code
pyPython
import re
pattern = re.compile(r"^#[0-9a-fA-F]{6}$")
input_text = "#ff5733"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
How the pattern works
^# requires the leading hash. [0-9a-fA-F]{6} requires exactly 6 hexadecimal digits. $ anchors the end. Note this is STRICTER than the general hex-color pattern: the HTML5 color input only accepts the 6-digit form, not the 3-digit shorthand.
Examples
Input
#ff5733Matches
#ff5733
Input
#0F0F0FMatches
#0F0F0F
Input
#fffNo match
—