Validate categories with regular expression

2

I'd like to know how to validate categories with regular expressions. So I have a list of categories separated by commas. Each word must have a maximum of 20 characters, I need to validate each comma if the word has alphanumeric characters.

I need to release the spaces as I did below, but the category must be at least 1 character, but it can not be made of space.

  

Nothing, look ---- That can not happen to be space between the commas.

I started doing this:

^[a-zA-Z0-9 ]{1,20}$
  

Car, motorcycle, knife, nothing

Someone has an idea how I can do this.

    
asked by anonymous 08.08.2015 / 23:17

1 answer

1

I've run tests here, and I believe this is the answer to my question:

^[\s]*[\w]+[\s]*(?:,[\s]*[\w]*[\s]*)*$

Even though it accepts 2 commas together, I let the user make a mistake and then hit it ...

    
09.08.2015 / 00:39