In my asp.net page, I have an input box that has to have the following validation on it:
在我的asp.net页面中,我有一个输入框,它必须有以下的验证:
Must be alphanumeric, with at least 1 character (i.e. can't be ALL numbers).
必须是字母数字,至少有一个字符(即不能是所有数字)。
6 个解决方案
#1
53
^\d*[a-zA-Z][a-zA-Z0-9]*$
Basically this means:
基本上这意味着:
- Zero or more ASCII digits;
- 0或更多的ASCII数字;
- One alphabetic ASCII character;
- 一个字母ASCII字符;
- Zero or more alphanumeric ASCII characters.
- 零或更多的字母数字ASCII字符。
Try a few tests and you'll see this'll pass any alphanumeric ASCII string where at least one non-numeric ASCII character is required.
尝试一些测试,您将看到这将传递任何一个字母数字ASCII字符串,其中至少需要一个非数字ASCII字符。
The key to this is the \d*
at the front. Without it the regex gets much more awkward to do.
关键在于前面的\d*。没有它,regex将变得更加笨拙。
#2
18
Most answers to this question are correct, but there's an alternative, that (in some cases) offers more flexibility if you want to change the rules later on:
对这个问题的大多数回答都是正确的,但是有另外一种选择,如果你想在以后改变规则的话,(在某些情况下)会提供更多的灵活性:
^(?=.*[a-zA-Z].*)([a-zA-Z0-9]+)$
This will match any sequence of alphanumerical characters, but only if the first group also matches the whole sequence. It's a little-known trick in regular expressions that allows you to handle some very difficult validation problems.
这将匹配任何字母数字字符序列,但前提是第一组也匹配整个序列。在正则表达式中,这是一个鲜为人知的技巧,它允许您处理一些非常困难的验证问题。
For example, say you need to add another constraint: the string should be between 6 and 12 characters long. The obvious solutions posted here wouldn't work, but using the look-ahead trick, the regex simply becomes:
例如,假设您需要添加另一个约束:字符串长度应该在6到12个字符之间。这里发布的显而易见的解决方案是行不通的,但是使用前面的技巧,regex就变成了:
^(?=.*[a-zA-Z].*)([a-zA-Z0-9]{6,12})$
#3
4
^[\p{L}\p{N}]*\p{L}[\p{L}\p{N}]*$
Explanation:
解释:
-
[\p{L}\p{N}]*
matches zero or more Unicode letters or numbers - [\p{}]*匹配0或更多的Unicode字母或数字。
-
\p{L}
matches one letter - 一个字母\ p { L }匹配
-
[\p{L}\p{N}]*
matches zero or more Unicode letters or numbers - [\p{}]*匹配0或更多的Unicode字母或数字。
-
^
and$
anchor the string, ensuring the regex matches the entire string. You may be able to omit these, depending on which regex matching function you call. - $锚定字符串,确保正则表达式匹配整个字符串。您可以省略这些,这取决于您调用的regex匹配函数。
Result: you can have any alphanumeric string except there's got to be a letter in there somewhere.
结果:你可以有任何字母数字字符串,除非有一个字母在某处。
\p{L}
is similar to [A-Za-z]
except it will include all letters from all alphabets, with or without accents and diacritical marks. It is much more inclusive, using a larger set of Unicode characters. If you don't want that flexibility substitute [A-Za-z]
. A similar remark applies to \p{N}
which could be replaced by [0-9]
if you want to keep it simple. See the MSDN page on character classes for more information.
{L}与[a-z -z]类似,但它将包括所有字母的所有字母,带有或没有重音符号和区别符号。它更具有包容性,使用更大的Unicode字符。如果你不希望这种灵活性替代[A-Za-z]。如果你想保持简单,也可以用[0-9]替换的类似语句。有关更多信息,请参见字符类的MSDN页面。
The less fancy non-Unicode version would be
不那么花哨的非unicode版本将会是。
^[A-Za-z0-9]*[A-Za-z][A-Za-z0-9]*$
#4
1
^\w*[\p{L}]\w*$
This one's not that hard. The regular expression reads: match a line starting with any number of word characters (letters, numbers, punctuation (which you might not want)), that contains one letter character (that's the [\p{L}]
part in the middle), followed by any number of word characters again.
这个没那么难。正则表达式的意思是:匹配一行开头的字符数(字母、数字、标点符号(你可能不需要)),其中包含一个字母字符(这是中间的[\p{L}]),后面跟着任意数量的单词字符。
If you want to exclude punctuation, you'll need a heftier expression:
如果要排除标点符号,则需要一个heftier表达式:
^[\p{L}\p{N}]*[\p{L}][\p{L}\p{N}]*$
And if you don't care about Unicode you can use a boring expression:
如果你不关心Unicode,你可以使用一个无聊的表达式:
^[A-Za-z0-9]*[A-Za-z][A-Za-z0-9]*$
#5
1
^[0-9]*[A-Za-z][0-9A-Za-z]*$
is the regex that will do what you're after. The ^ and $ match the start and end of the word to prevent other characters. You could replace the [0-9A-z] block with \w, but i prefer to more verbose form because it's easier to extend with other characters if you want.
你所追求的是正则表达式。$匹配字的开头和结尾,以防止其他字符。您可以用\w替换[0-9A-z]块,但我更喜欢更详细的形式,因为如果您愿意,可以更容易地扩展其他字符。
Add a regular expression validator to your asp.net page as per the tutorial on MSDN: http://msdn.microsoft.com/en-us/library/ms998267.aspx.
根据MSDN上的教程,在asp.net页面中添加一个正则表达式验证器:http://msdn.microsoft.com/en-us/library/ms998267.aspx。
#6
0
^[0-9]*[a-zA-Z][a-zA-Z0-9]*$
Can be
可以
- any number ended with a character,
- 任何数字以一个字符结束,
- or an alphanumeric expression started with a character
- 或者字母数字的表达式是从一个字符开始的。
- or an alphanumeric expression started with a number, followed by a character and ended with an alphanumeric subexpression
- 或字母数字表达式以数字开头,后面跟着字符,以字母数字子表达式结束。
#1
53
^\d*[a-zA-Z][a-zA-Z0-9]*$
Basically this means:
基本上这意味着:
- Zero or more ASCII digits;
- 0或更多的ASCII数字;
- One alphabetic ASCII character;
- 一个字母ASCII字符;
- Zero or more alphanumeric ASCII characters.
- 零或更多的字母数字ASCII字符。
Try a few tests and you'll see this'll pass any alphanumeric ASCII string where at least one non-numeric ASCII character is required.
尝试一些测试,您将看到这将传递任何一个字母数字ASCII字符串,其中至少需要一个非数字ASCII字符。
The key to this is the \d*
at the front. Without it the regex gets much more awkward to do.
关键在于前面的\d*。没有它,regex将变得更加笨拙。
#2
18
Most answers to this question are correct, but there's an alternative, that (in some cases) offers more flexibility if you want to change the rules later on:
对这个问题的大多数回答都是正确的,但是有另外一种选择,如果你想在以后改变规则的话,(在某些情况下)会提供更多的灵活性:
^(?=.*[a-zA-Z].*)([a-zA-Z0-9]+)$
This will match any sequence of alphanumerical characters, but only if the first group also matches the whole sequence. It's a little-known trick in regular expressions that allows you to handle some very difficult validation problems.
这将匹配任何字母数字字符序列,但前提是第一组也匹配整个序列。在正则表达式中,这是一个鲜为人知的技巧,它允许您处理一些非常困难的验证问题。
For example, say you need to add another constraint: the string should be between 6 and 12 characters long. The obvious solutions posted here wouldn't work, but using the look-ahead trick, the regex simply becomes:
例如,假设您需要添加另一个约束:字符串长度应该在6到12个字符之间。这里发布的显而易见的解决方案是行不通的,但是使用前面的技巧,regex就变成了:
^(?=.*[a-zA-Z].*)([a-zA-Z0-9]{6,12})$
#3
4
^[\p{L}\p{N}]*\p{L}[\p{L}\p{N}]*$
Explanation:
解释:
-
[\p{L}\p{N}]*
matches zero or more Unicode letters or numbers - [\p{}]*匹配0或更多的Unicode字母或数字。
-
\p{L}
matches one letter - 一个字母\ p { L }匹配
-
[\p{L}\p{N}]*
matches zero or more Unicode letters or numbers - [\p{}]*匹配0或更多的Unicode字母或数字。
-
^
and$
anchor the string, ensuring the regex matches the entire string. You may be able to omit these, depending on which regex matching function you call. - $锚定字符串,确保正则表达式匹配整个字符串。您可以省略这些,这取决于您调用的regex匹配函数。
Result: you can have any alphanumeric string except there's got to be a letter in there somewhere.
结果:你可以有任何字母数字字符串,除非有一个字母在某处。
\p{L}
is similar to [A-Za-z]
except it will include all letters from all alphabets, with or without accents and diacritical marks. It is much more inclusive, using a larger set of Unicode characters. If you don't want that flexibility substitute [A-Za-z]
. A similar remark applies to \p{N}
which could be replaced by [0-9]
if you want to keep it simple. See the MSDN page on character classes for more information.
{L}与[a-z -z]类似,但它将包括所有字母的所有字母,带有或没有重音符号和区别符号。它更具有包容性,使用更大的Unicode字符。如果你不希望这种灵活性替代[A-Za-z]。如果你想保持简单,也可以用[0-9]替换的类似语句。有关更多信息,请参见字符类的MSDN页面。
The less fancy non-Unicode version would be
不那么花哨的非unicode版本将会是。
^[A-Za-z0-9]*[A-Za-z][A-Za-z0-9]*$
#4
1
^\w*[\p{L}]\w*$
This one's not that hard. The regular expression reads: match a line starting with any number of word characters (letters, numbers, punctuation (which you might not want)), that contains one letter character (that's the [\p{L}]
part in the middle), followed by any number of word characters again.
这个没那么难。正则表达式的意思是:匹配一行开头的字符数(字母、数字、标点符号(你可能不需要)),其中包含一个字母字符(这是中间的[\p{L}]),后面跟着任意数量的单词字符。
If you want to exclude punctuation, you'll need a heftier expression:
如果要排除标点符号,则需要一个heftier表达式:
^[\p{L}\p{N}]*[\p{L}][\p{L}\p{N}]*$
And if you don't care about Unicode you can use a boring expression:
如果你不关心Unicode,你可以使用一个无聊的表达式:
^[A-Za-z0-9]*[A-Za-z][A-Za-z0-9]*$
#5
1
^[0-9]*[A-Za-z][0-9A-Za-z]*$
is the regex that will do what you're after. The ^ and $ match the start and end of the word to prevent other characters. You could replace the [0-9A-z] block with \w, but i prefer to more verbose form because it's easier to extend with other characters if you want.
你所追求的是正则表达式。$匹配字的开头和结尾,以防止其他字符。您可以用\w替换[0-9A-z]块,但我更喜欢更详细的形式,因为如果您愿意,可以更容易地扩展其他字符。
Add a regular expression validator to your asp.net page as per the tutorial on MSDN: http://msdn.microsoft.com/en-us/library/ms998267.aspx.
根据MSDN上的教程,在asp.net页面中添加一个正则表达式验证器:http://msdn.microsoft.com/en-us/library/ms998267.aspx。
#6
0
^[0-9]*[a-zA-Z][a-zA-Z0-9]*$
Can be
可以
- any number ended with a character,
- 任何数字以一个字符结束,
- or an alphanumeric expression started with a character
- 或者字母数字的表达式是从一个字符开始的。
- or an alphanumeric expression started with a number, followed by a character and ended with an alphanumeric subexpression
- 或字母数字表达式以数字开头,后面跟着字符,以字母数字子表达式结束。