简体   繁体   English

正则表达式未检测到用户名文本框中的空间

[英]regex not detecting space in username textbox

I have a asp:textbox taking a Username that is part of a Signup form for a new user account. 我有一个asp:textbox,其用户名是新用户帐户的注册表单的一部分。

Obviously I don't want the user to sign up using a space as a name so I have this regular expression which should keep the valid entry to ASCII characters between 3 and 16 in length with NO SPACES. 显然,我不希望用户使用空格作为名称进行注册,因此我拥有此正则表达式,该表达式应将有效输入的ASCII字符的长度保持在3到16之间,并且没有空格。

but the no spaces doesn't work in practice. 但实际上没有空格是行不通的。 it works in Regex editors and checkers but not my aspx page. 它适用于Regex编辑器和检查器,但不适用于我的aspx页面。

Any suggestions? 有什么建议么?

^([a-zA-Z0-9!@#$%^&*()-_=+;:'"|~`<>?/{}]{3,16})$|\s

many thanks 非常感谢

^([a-zA-Z0-9!@#$%^&*()-_=+;:'"|~`?/{}]{3,16})$|\s

Your current regex says: "I match a string if the string is made of these characters and is 3 to 16 characters in length OR if it contains a whitespace character." 您当前的正则表达式说: “如果字符串由这些字符组成并且长度为3至16个字符, 或者如果包含空格字符,匹配字符串。”

So, if you don't want it to match spaces, remove |\\s (ie the 'or' operator and the whitespace pattern) from the regex. 因此,如果您不希望它与空格匹配,请从正则表达式中除去|\\s (即'or'运算符和空白模式)。

It seems your having trouble understanding what dtb is trying to say. 您似乎无法理解dtb想要说的话。 Let me break-down the regex for you and you will see what he is saying: 让我为您分解正则表达式,您将明白他在说什么:

^ - matches the beginning of the input string
( - begins a capture group, in your case useless and can be removed along with the closing ) just before the $
[ - begins a group of characters
a-zA-Z0-9!@#$%^&*()-_=+;:'"|~`?/{} - defines all the characters allowed, NOTICE there is no space character so spaces will not count
] - ends the group of characters
{3,16} - says that the preceding character(or group of characters in this case) must occur between 3 and 16 times
) - closes the capture group, again can be removed with the open (
$ - matches the end of the input string

This is where your expression goes awry... 这是您的表情出现问题的地方...

| - says that the preceeding match expression (this is the $ which is the end of input) OR the following must be true, but not necessarily both
\s - matches a space or tab anywhere in the input string

So (if I'm reading this correctly) your regex states: 所以(如果我没看错的话)您的正则表达式指出:

"I match a string if the string starts with ascii characters and is 3 to 16 characters in length before it finds either the end of the string or some whitespace (tab or space)." “如果字符串以ascii字符开头并且长度为3到16个字符,那么它会在找到字符串的末尾或某些空格(制表符或空格)之前与之匹配。”

To fix it, remove the '|\\s' from the end of your expression and just use the following: 要解决此问题,请从表达式末尾删除“ | \\ s”,然后使用以下命令:

^([a-zA-Z0-9!@#$%^&*()-_=+;:'"|~`<>?/{}]{3,16})$

A simpler answer would be: 一个简单的答案是:

^([^\s]{3,16})$

which means "the whole string must consist of three to sixteen repeats of anything other than whitespace." 这表示“整个字符串必须包含3到16个除空白以外的任何重复。”

That will allow accented characters as well, but that's probably something you'll need to accept anyway. 这也将允许带重音的字符,但是无论如何,这可能是您需要接受的。

更简单的是

^\S{3,16}$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM