-2

I have a label name called "Color Scheme" and an input showing you the HEX colors that you just inputted.

I have limited the max length to 27 so when they input their HEX colors, like this 222222,000000,999999,ffeb00. By default, I put the commas in myself, but I want the regex to automatically input a comma to the end of the HEX color and only allow letters, numbers, and commas.

Does anyone the regex code for this?

Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • 1
    Show us the `regex` you've tried – Tushar Jul 16 '15 at 14:47
  • @Tushar I used this code `/^\d(?:,\d)*$/`. I got this code [here](http://stackoverflow.com/a/18033103/5124044) – Brian Anderson Jul 16 '15 at 14:55
  • Consider the string "FFEEBB" - is that a single hex-number? Why should it not be interpreted as "FFE,EBB" in the abbreviated form and how shuld the regex recognize end of hex? Just every 6 chars? – MBaas Jul 16 '15 at 14:56
  • @MBaas, its not a single hex-number. I made the input to only have 4 inputs of HEX colors by limiting the max length. And I needed was the regex, but my answer is already solved. – Brian Anderson Jul 16 '15 at 15:07

1 Answers1

0

If hex values are always 6 chars long, try this:

^([0-9a-fA-F]{6}(,|$)){4}

See live demo.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Also use `case-insensitive` flag – Tushar Jul 16 '15 at 14:57
  • @Tushar the regex is case insensitive already. using the flag takes 1 more character in the regex `A-F` vs `(?i)`, or requires using a modifier flag parameter, which not all languages support. This regex works everywhere as is. – Bohemian Jul 16 '15 at 15:03