接受字母数字字符(6-10 个字符)的正则表达式 .NET、C#
我正在使用 C# 和 .NET 构建用户注册表单。 我需要验证用户输入的密码字段。 验证要求如下。
- 它应该是字母数字(az,AZ,0-9)
- 它应该接受6-10个字符(最少6个字符,最多10个字符)
- 至少有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.
- It should be alphanumeric (a-z , A-Z , 0-9)
- It should accept 6-10 characters (minimum 6 characters, maximum 10 characters)
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果可以的话,通过多个正则表达式传递它。它会比那些前瞻怪物干净得多:-)
虽然有些人可能认为它很聪明,但没有必要用一个正则表达式(或者甚至使用任何正则表达式,有时 - 只是)所有事情见证那些想要正则表达式检测 75 到 4093 之间数字的人)。
您是否愿意看到一些干净的代码,例如:
或类似的内容:
我知道我更喜欢维护哪一个:-)
Pass it through multiple regexes if you can. It'll be a lot cleaner than those look-ahead monstrosities :-)
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:
or something like:
I know which one I'd prefer to maintain :-)
使用正向lookahead
环视的语法:
(?=REGEX)
正向前瞻(?!REGEX)
负向前瞻(?<=REGEX)
正向后向(? 负向后查找
Use positive lookahead
The syntax for look around:
(?=REGEX)
Positive lookahead(?!REGEX)
Negative lookahead(?<=REGEX)
Positive lookbehind(?<!REGEX)
Negative lookbehindhttp://www.regular-expressions.info/refadv.html
http://www.regular-expressions.info/lookaround.html
http://www.regular-expressions.info/refadv.html
http://www.regular-expressions.info/lookaround.html