Reputation: 169
I'm trying to disallow commas in a string entered into a textbox. Here is what I have so far:
[RegularExpression (@"?[^,]*$",
ErrorMessage = "Commas are not allowed in the subtask title. Please remove any and try again")]
This is probably my 5th or 6th attempt, nothing so far has worked. Any help would be appreciated.
Upvotes: 7
Views: 19419
Reputation: 5307
Try changing your regex to:
"^[^,]+$"
Let's say we're matching against "Hello, world"
The first ^
asserts that we're at the beginning of the string. Next [^,]
is a character class which means "Any character except ,
." +
next to something means "Match this one or more times." Finally, $
asserts that we're now at the end of the string.
So, this regular expression means "At the start of the string (^
), match any character that's not a comma ([^,]
) one or more times (+
) until we reach the end of the string ($
).
This regular expression will fail on "Hello, world"
- everything will be fine for H
, e
, l
, l
, and o
, until we reach the comma - at which point the character class fails to match "not a comma".
For some great tutorials about regular expressions, you should read up on http://www.regular-expressions.info/
Upvotes: 25