I would like to validate a user’s input of indexing tags for posts by running them through a regular expression in Codeigniter’s validation library.
Ideally, the regex would have the following conditions:
- allow only letters and numbers
- tags will be considered unique entities if separated by space (any number) or comma (any number)
- there should be a limit to the number of tags (for ex in this case 6)
There are a few partial solutions to this here on SO:
but they do not fully resolve my issue.
Any suggestions?
If all you want is validating, then this should do:
Same regex, but with comments and whitespace:
To allow input with leading/trailing spaces/commas, change regex to this:
and to further allow input with zero tags, change regex to this: