kmarks2
kmarks2

Reputation: 4875

Regex Validators in asp.net - validate all strings except those with a specific character

I'd like to use a asp:RegularExpressionValidator where the ValidatorExpression is a regex that matches all strings that do not contain an ampersand. Google fu didn't yield much, but I'm sure it's probably not too complicated.

<asp:RegularExpressionValidator
   ID="StringValidator1"
   runat="server"
   ControlToValidate="textBox1"
   ValidationExpression="???"
   Display="Dynamic"
   ErrorMessage="String cannot contain ampersands"
   ValidationGroup="Group1"
/>

Thanks.

Upvotes: 0

Views: 442

Answers (2)

Tim Pietzcker
Tim Pietzcker

Reputation: 336098

ValidationExpression = "^[^&]*$"

matches any string that doesn't contain an ampersand.

Explanation:

^      # Start of string
[^&]*  # Any number of characters that are not ampersands
$      # End of string

Upvotes: 3

Sabuj Hassan
Sabuj Hassan

Reputation: 39355

Your regex will be

^[^&]+$

Means everything except &

Upvotes: 1

Related Questions