I am using SobiPro, a directory system for joomla and I have a field that will have values that contain alphanumerics and hyphens only, so a sample of what might be in this
Try this:
^[-\w\s]+(?:,[-\w\s]*)*$
Using ^
and $
ensures that we validate the entire value, and don't just find a match somewhere within.
The first character class, [-\w\s]+
allows one or more alphanumeric, whitespace, or dash characters. The dash should go first in the class brackets.
The second group allows zero or more repetitions with separating commas. It is wrapped in non-capturing parentheses, a small performance optimization: (?: … )*
Notes:
A,B,,D
. If you don't want to allow this, change the second-to-last *
to a +
.\w
shorthand allows underscores. To prevent this, replace them with A-Za-z0-9
.