| Viewed 135 times

How to check a string is a valid hex code in javaScript?


A hex code must begin with a pound key # and is exactly 6 characters in length. Each character must be a digit from 0-9 or an alphabetic character from A-F. All alphabetic characters may be uppercase or lowercase.

1 Answer
Mohammad Qandeel
Answered
24

There are many ways to do this, you can try this function, it will help you to check a string is a valid hex code in javaScript

function isValidHexCode(str) {
 return /^#[a-f0-9]{6}$/i.test(str);
}


Related Questions