Reputation: 1030
I am working with ASP.NET 3.5, VB.NET, and I am trying to validate some inputs through a regular expression.
My expression is:
^[a-zA-Z'_''-''/'' ''\''@''&''.'',''ç''Ç''ö''Ö''I''ı''i''İ''ğ''Ğ''ş''Ş''ü''Ü'\s\d]{1,50}$
And it works for all the special characters I included except for the "-". It just doesn't work. I tried Internet Explorer and Firefox, same problem. How can it be fixed?
Upvotes: 0
Views: 1403
Reputation: 338208
Why all the single quotes? They are unnecessary.
^[-a-zA-Z_/\\@&.,çÇöÖIiiIgGsSüÜ\s\d]{1,50}$ ^ ^^
Move the "-" to the start or the end of the character class to have it recognized. Also, the backslash needs to be escaped properly, or you won't be able to match backslashes either.
See the "^" marks above.
Upvotes: 3
Reputation: 415735
The - character has special meaning in that context. Escape it with a backslash or list it as the last character in the block.
Upvotes: 3
Reputation: 58441
You have to escape the - character by placing \ in front of it. The regex would then become
^[a-zA-Z'_''\-''/'' ''\''@''&''.'',''ç''Ç''ö''Ö''I''ı''i''İ''ğ''Ğ''ş''Ş''ü''Ü'\s\d]{1,50}$
Upvotes: 1
Reputation: 391336
In a regular expression character group [...]
, a minus sign means a range specifying a range of legal characters. Useful if you have lots of sequential symbols (all the letters, the digits, etc.) and don't want to list them all.
Example: [0-9]
This will match all the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9, but not the minus sign.
To match a minus sign directly, prefix it with a backslash:
Example: [0\-9]
This will match the digit 0, the minus sign, or the digit 9.
Upvotes: 1