I need to include a certain restriction in the username validation.
A username can only contain the following characters in its name:
- a-z
- A-Z
- 0-9
- ._-
In addition, I want to display failed matches in the view so that the user can change his input.
Thus, the usual regex would be: @"^[a-z0-9._-]$"
(ignoreCase).
But this pattern can’t tell me exactly which match was unsuccessful.
To satisfy my additional requirement, I have to do the following:
@"^(?:(?<letter>[a-z])|(?<numeric>[0-9])|(?<character>[._-]))*$"
.
But this will work if the match is successful. If the match fails, I can’t get which group didn’t work.
So I have to invert the pattern and match successful when input is wrong. And here I can’t understand how to properly use negate in order to get right pattern…
I figure that if I invert the regex match, I can figure out which character the user entered incorrectly and output a list of the rules they violated
For example:
- only [a-z]
- only . _ –
If the user entered : someNonLatinWord
Is there a better approach?