user3614070
user3614070

Reputation: 141

Allow Hyphens and apostraphes but no other special characters

I have the following validation expresion on an asp.net web form that allows alphanumeric characters, spaces,at least one alpha character, and a minimum of 3 characters and a maximum of 20: ValidationExpression="(?!^[0-9]$)(?!^[a-zA-Z]$)^([a-zA-Z0-9 _]{3,20})$"

Now I have been asked to allow hyphens and apostraphes but no other special characters.

How can I implement this in my current validation?

Upvotes: 0

Views: 142

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627219

This (?!^[0-9'-]$)(?!^[a-zA-Z'-]$)^([a-zA-Z0-9 _'-]{3,20})$?

Well, the main trick here is that - sign should be placed at the end of the character group for it to be parsed as a literal hyphen.

Upvotes: 1

heemayl
heemayl

Reputation: 42107

Try this:

(?=.*?[A-Za-z]+)^[a-zA-Z0-9_\-' ]{3,20}$

Upvotes: 1

Related Questions