Reputation: 2855
I am using the following regular expression on my regular expression validator
<asp:RegularExpressionValidator ID="revAddress" runat="server" ControlToValidate="txtAddress" ValidationExpression="^[a-zA-Z0-9 ]+$" ErrorMessage="Alphabets and Numbers only" ForeColor="Red"> </asp:RegularExpressionValidator>
The expression being - ^[a-zA-Z0-9 ]+$
This allows letters, numbers and spaces and I tried . \, and it wasn't working Now, wondering how to include these special characters.
dot (.) Comma(,) hypen (-) and slash (/)
I appreciate your help.
Upvotes: 0
Views: 4923
Reputation: 43663
You need to list -
as the first or the last character within [...]
^[a-zA-Z0-9 .,/-]+$
or
[-a-zA-Z0-9 .,/]+$
as in between it has range meaning, as you used it for a-z
, A-Z
and 0-9
.
Other option is to escape -
by \
^[a-zA-Z0-9 .,\-/]+$
Some environments require also /
character to be escaped. As escaping does not hurt, you should go with
^[a-zA-Z0-9 .,\-\/]+$
Upvotes: 1
Reputation: 106375
This should do the trick, I suppose:
ValidationExpression="^[a-zA-Z0-9 ./,-]+$"
Of the symbols mentioned, only '-'
(hyphen) can have the special meaning inside the regex character class - but only if it's not the first or the last one there (it's used to specify a range of characters).
In other words, it shouldn't be written like this:
ValidationExpression="^[a-zA-Z0-9 .-/,]+$"
^^^ <- parsed as 'characters between . and /'
Upvotes: 1
Reputation: 123377
just add those characters in your class (escaping the hyphen and /
)
^[a-zA-Z0-9,.\/\-]+$
example jsbin : http://jsbin.com/eweren/2/edit
Upvotes: 2