Valid Hex Code

Published by Helen Yu in

Create a function that determines whether a string is a valid hex code.

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.

Examples

is_valid_hex_code("#CD5C5C") ➞ true

is_valid_hex_code("#EAECEE") ➞ true

is_valid_hex_code("#eaecee") ➞ true

is_valid_hex_code("#CD5C58C") ➞ false
# Length exceeds 6

is_valid_hex_code("#CD5C5Z") ➞ false
# Not all alphabetic characters in A-F

is_valid_hex_code("#CD5C&C") ➞ false
# Contains unacceptable character

is_valid_hex_code("CD5C5C") ➞ false
# Missing #

Notes

N/A

Watch a quick demo on how Edabit works.