js regex validate rgb hex color value
JavaScript[Edit]
+
0
-
0
js regex validate rgb hex color value
1 2 3 4 5 6 7 8 9 10 11 12 13const regex = /^#([A-F0-9]{6}|[A-F0-9]{3})$/gi; const color = '#ffffff'; const result = color.match(regex); if (result) { console.log('Color is correct!'); } else { console.log('Color is incorrect!'); } // Accepts standard hex color code e.g. '#A52A2A', '#ffffff', '#000000' ... // + shorthand e.g. '#000', '#FFF', '#fff' etc.
[Edit]
+
0
-
0
js regex validate rgb hex color value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16const regex = /^#([A-F0-9]{6}|[A-F0-9]{3})$/gi; const validateColor = (color) => !!color.match(regex); // Usage example: if (validateColor('#ffffff')) { console.log('Color is correct!'); } else { console.log('Color is incorrect!'); } // Accepts standard hex color code e.g. '#A52A2A', '#ffffff', '#000000' ... // + shorthand e.g. '#000', '#FFF', '#fff' etc.