接受字母数字字符(6-10 个字符)的正则表达式 .NET、C#

发布于 2024-08-19 22:36:29 字数 290 浏览 2 评论 0原文

我正在使用 C# 和 .NET 构建用户注册表单。 我需要验证用户输入的密码字段。 验证要求如下。

  1. 它应该是字母数字(az,AZ,0-9)
  2. 它应该接受6-10个字符(最少6个字符,最多10个字符)
  3. 至少有1个字母和数字(例如:stack1over

我是使用如下正则表达式。

^([a-zA-Z0-9]{6,10})$

它满足我的前两个条件。 当我只输入字符或数字时,它会失败。

I am building a user registration form using C# with .NET.
I have a requirement to validate user entered password fields.
Validation requirement is as below.

  1. It should be alphanumeric (a-z , A-Z , 0-9)
  2. It should accept 6-10 characters (minimum 6 characters, maximum 10 characters)
  3. With at least 1 alphabet and number (example: stack1over)

I am using a regular expression as below.

^([a-zA-Z0-9]{6,10})$

It satisfies my first 2 conditions.
It fails when I enter only characters or numbers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蝶舞 2024-08-26 22:36:29

如果可以的话,通过多个正则表达式传递它。它会比那些前瞻怪物干净得多:-)

^[a-zA-Z0-9]{6,10}$
[a-zA-Z]
[0-9]

虽然有些人可能认为它很聪明,但没有必要用一个正则表达式(或者甚至使用任何正则表达式,有时 - 只是)所有事情见证那些想要正则表达式检测 75 到 4093 之间数字的人)。

您是否愿意看到一些干净的代码,例如:

if not checkRegex(str,"^[0-9]+$")
    return false
val = string_to_int(str);
return (val >= 75) and (val <= 4093)

或类似的内容:

return checkRegex(str,"^7[5-9]$|^[89][0-9]$|^[1-9][0-9][0-9]$|^[1-3][0-9][0-9][0-9]$|^40[0-8][0-9]$|^409[0-3]$")

我知道我更喜欢维护哪一个:-)

Pass it through multiple regexes if you can. It'll be a lot cleaner than those look-ahead monstrosities :-)

^[a-zA-Z0-9]{6,10}$
[a-zA-Z]
[0-9]

Though some might consider it clever, it's not necessary to do everything with a single regex (or even with any regex, sometimes - just witness the people who want a regex to detect numbers between 75 and 4093).

Would you rather see some nice clean code like:

if not checkRegex(str,"^[0-9]+$")
    return false
val = string_to_int(str);
return (val >= 75) and (val <= 4093)

or something like:

return checkRegex(str,"^7[5-9]$|^[89][0-9]$|^[1-9][0-9][0-9]$|^[1-3][0-9][0-9][0-9]$|^40[0-8][0-9]$|^409[0-3]$")

I know which one I'd prefer to maintain :-)

寒冷纷飞旳雪 2024-08-26 22:36:29

使用正向lookahead

^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]{6,10}$

环顾四周也称为零宽度断言。它们是零宽度的,就像行的开头和结尾一样(^$)。不同之处在于,lookarounds 实际上会匹配字符,但随后放弃匹配,只返回结果:匹配或不匹配。这就是为什么它们被称为“断言”。它们不消耗字符串中的字符,而仅断言是否可能匹配。

环视的语法:

  • (?=REGEX) 正向前瞻
  • (?!REGEX) 负向前瞻
  • (?<=REGEX) 正向后向
  • 查找(? 负向后查找

Use positive lookahead

^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]{6,10}$

Look arounds are also called zero-width assertions. They are zero-width just like the start and end of line (^, $). The difference is that lookarounds will actually match characters, but then give up the match and only return the result: match or no match. That is why they are called "assertions". They do not consume characters in the string, but only assert whether a match is possible or not.

The syntax for look around:

  • (?=REGEX) Positive lookahead
  • (?!REGEX) Negative lookahead
  • (?<=REGEX) Positive lookbehind
  • (?<!REGEX) Negative lookbehind
无风消散 2024-08-26 22:36:29
string r = @"^(?=.*[A-Za-z])(?=.*[0-9])[A-Za-z0-9]{6,10}$";
Regex x = new Regex(r);
var z = x.IsMatch(password);

http://www.regular-expressions.info/refadv.html

http://www.regular-expressions.info/lookaround.html

string r = @"^(?=.*[A-Za-z])(?=.*[0-9])[A-Za-z0-9]{6,10}$";
Regex x = new Regex(r);
var z = x.IsMatch(password);

http://www.regular-expressions.info/refadv.html

http://www.regular-expressions.info/lookaround.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文