Check whether text is a valid hex code


How would I check if a text (that will be input by the user) is a hex color value? I need this to only allow valid hex values.

Thanks in advance.

I solved this in my own way but would still like to know if there is a better way:

I just created a list with each value that can be in a hex color value and check whether each letter in the text is in the list. If they are then I continue.

Why not post what you have done so others can learn.

The rules to be a valid hexadecimal code are:

  1. It should start from ‘#’ symbol.
  2. It should be followed by the letters from a-f, A-F and/or digits from 0-9.
  3. The length of the hexadecimal color code should be either 6 or 3, excluding ‘#’ symbol.

I did Peter, In the second line I describe my method. I will add my blocks as soon as I complete further checks (length, inclusion of ‘#’ etc)

This is what I am doing, apart from the 3 values at the moment. Thanks for the info!

Your welcome!

I hate posting my blocks since Im very messy and probably dont work in the most efficient way but here it is.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.