I have a problem.
I want a user to be allowed to write everything you can SEE on a Swedish keyboard (not using a character map or similar). This means all English alphanumeric characters and åäö. The non-alphanumeric characters which is allowed is §½!"@#£¤$%&{/()[]=}?+\´`^ and so on.
My expression is:
[\wåäö§½!"@#£¤$%&/{()=}?\\\[\]+´`^¨~'*,;.:\-_<>|]
In C# it looks like this:
Regex allowedChars = new Regex("@[\\wåäö§½!\"@#£¤$%&/{()=}?\\\\[\\]+´`^¨~'*,;.:\\-_<>|]");
I check it with:
if (allowedChars.IsMatch(mTextBoxUserName.Text.Trim()))
The problem is that if i write a faulty character together with an allowed character the if statement think its a match. I want it to match it for the entire word. I tried adding a “+” at the end of the expression but then it never matched…
Any ideas?
Two things:
Your string erroneously has the
@character inside rather than before the string. This may have been a copy paste error, or it might not have been.You’re only checking that one of the allowed characters is present, rather than ONLY the allowed characters. You can use anchoring and repetition to solve this problem: