Allow - (dash) in regular expression Allow - (dash) in regular expression asp.net asp.net

Allow - (dash) in regular expression


The dash needs to be the first/last character in the character class in order to be used literally:

^[-0-9a-zA-Z \/_?:.,\s]+$^[0-9a-zA-Z \/_?:.,\s-]+$

You could also escape it, if not the first/last:

^[0-9a-zA-Z\- \/_?:.,\s]+$


Simple answer, user \- in character class.

^[0-9a-zA-Z\- \/_?:.,\s]+$


Escape it with \ like:

^[\-0-9a-zA-Z \/_?:.,\s]+$