How to check for special characters using regex
.net, c#, regex
Solution
There's a few things wrong with your expression. First you don't have the start string character `^` and end string character `$` at the beginning and end of your expression meaning that it only has to find a match somewhere within your string.
Second, you're only looking for one character currently. To force a match of all the characters you'll need to use `*` Here's what it should be:
Regex objAlphaPattern = new Regex(@"^[a-zA-Z0-9_@.-]*$");
bool sts = objAlphaPattern.IsMatch(username);
Problem
NET. I have created a regex validator to check for special characters means I donot want any special characters in username. The following is the code ``` Regex objAlphaPattern = new Regex(@"[a-zA-Z0-9_@.-]"); bool sts = objAlphaPattern.IsMatch(username); ``` If I provide username as $%^&asghf then the validator gives as invalid data format which is the result I want but If I provide a data s23_@.-^&()%^$# then my validator should block the data but my validator allows the data which is wrong So how to not allow any special characters except a-z A-A 0-9 _ @ .- Thanks Sunil Kumar Sahoo