validateHex
The validateHex() function validates a hexadecimal color code string.
Parameters
| Parameter | Type | Description |
|---|---|---|
hex | String | The input hexadecimal color code to be validated. |
Returns
Boolean:trueifhexis valid,falseotherwise
Throws
Error: Ifhexis not a string
Examples
Valid hexes:
const utils = require('utils-core.js');
const hex = '#00F';
const validate = utils.format.validateHex(hex);
//Output: true
const utils = require('utils-core.js');
const hex = '#FF0000';
const validate = utils.format.validateHex(hex);
//Output: true
const utils = require('utils-core.js');
const hex = '#00F';
const validate = utils.format.validateHex(hex);
//Output: true
Invalid hexes:
const utils = require('utils-core.js');
const hex = '#xy12z4';
const validate = utils.format.validateHex(hex);
//Output: false
const utils = require('utils-core.js');
const hex = '#invalid';
const validate = utils.format.validateHex(hex);
//Output: false