I have a viewmodel named “Classification” to be used in my MVC website.
I’m trying to validate a property named “Description” on this viewmodel using a regular expression attribute.
The requirements for this “Description” property are:
- Allows only the following characters:
- Letters, numbers, spaces and any of the following characters _ -+ ( )
/\
- Letters, numbers, spaces and any of the following characters _ -+ ( )
So, these values should pass the validation:
- test
- test1
- test1_
- t(e_ s)t1 i/s \good+-
And these should fail:
- t,test
- t.test
- test!
- te!@#$%st
This is the regular expression I have so far:
^[a-zA-Z0-9 _+-\/\\\(\)]+$
All the expected values above are passing, but the first two values in the should-fail list (the comma and period values) are passing as well, which is wrong!! This is very frustrating.
Please let me know what I have missed in my regular expression.
Thanks a lot in advance,
M
You need to escape the ‘-‘ dash in your character class
without it you get: