JavaScript / ECMAScript

HTML5 Color Input Value in JS

Validate the value of an HTML5 `<input type="color">` — exactly 6 hex digits with leading hash.

Try it in the JS tester →

Pattern

regexJS
^#[0-9a-fA-F]{6}$

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("^#[0-9a-fA-F]{6}$", "");
const input = "#ff5733";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

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

#ff5733

Matches

  • #ff5733

Input

#0F0F0F

Matches

  • #0F0F0F

Input

#fff

No match

Same pattern, other engines

← Back to HTML5 Color Input Value overview (all engines)