NET。我创建了一个正则表达式验证器来检查特殊字符意味着我DONOT希望用户名的任何特殊字符。以下是code
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);
如果我提供的用户名作为$%^&放大器; * asghf则验证程序提供了不正确的数据格式,它是我想要的结果,但如果我提供了一个数据S23 _ @ .- ^&放大器; *()%^ $#然后我的验证应该阻止的数据,但我可以验证这是错误的数据。
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
因此,如何不让除AZ AA任何特殊字符0-9 _ @ .-
So how to not allow any special characters except a-z A-A 0-9 _ @ .-
谢谢 苏尼尔·库马尔Sahoo
Thanks Sunil Kumar Sahoo
有几件事错了你的前pression。首先,你没有开始字符串的字符 ^
在你的前的开始和结束和结束串字符 $
pression这意味着它只有找到匹配的地方你的字符串中。
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);