需要一个正则表达式来验证长度为 6 到 25 个字符的字母数字

发布于 2024-12-04 04:02:04 字数 374 浏览 7 评论 0原文

我已经为我的密码字段表单验证创建了这个正则表达式,

"/^[[A-Za-z0-9]+[A-Za-z0-9, .!@#$%^&*()_]*]{3,25}$/",

它接受所有字母数字字符和特殊字符,但仅特殊字符是不可接受的。

问题出在长度检查:(

它应该像下面这样

Valid: aaaaaaaaa 
Valid: 111111111
Valid: 11111n11111
Valid: nnnn1jkhuig
InValid: @@@@@@@@

抛出错误

aaaaaaaaaaaa

,但它也会

i have created this regular expression for my form validation of password feild

"/^[[A-Za-z0-9]+[A-Za-z0-9, .!@#$%^&*()_]*]{3,25}$/",

it accepts all alphanumeric characters and special characters BUT special characters only is not acceptable .

The problem is with the length check :(

it should be like the following

Valid: aaaaaaaaa 
Valid: 111111111
Valid: 11111n11111
Valid: nnnn1jkhuig
InValid: @@@@@@@@

but it is throwing error on

aaaaaaaaaaaa

as well

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

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

发布评论

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

评论(4

红衣飘飘貌似仙 2024-12-11 04:02:04
^(?=.*[A-Za-z0-9])[A-Za-z0-9, .!@#$%^&*()_]{6,25}$

使用 PHP 测试)。解释:

  • 字符串应匹配 [A-Za-z0-9, .!@#$%^&*()_] 6 到 25 个字符
  • Somewhere字符串中的 [A-Za-z0-9] 必须存在(确保字符串不仅仅由特殊字符组成)。
^(?=.*[A-Za-z0-9])[A-Za-z0-9, .!@#$%^&*()_]{6,25}$

(Tested with PHP). The explanation:

  • The string should match [A-Za-z0-9, .!@#$%^&*()_] on 6 to 25 characters
  • Somewhere in the string [A-Za-z0-9] must be present (ensuring that the string is not composed of special chars only).
善良天后 2024-12-11 04:02:04

您可以使用零宽度肯定断言来解决此问题。这是正则表达式,我将在下面对其进行解构。

/(?=.*[A-Za-z0-9])[A-Za-z0-9, .!@#$%^&*()_]{3,25}/

第一个组件是 (?=.*[A-Za-z0-9])。构造 (?=...) 是一个零宽度断言,这意味着它会检查某些内容,但不会“吃掉”任何输出。如果“...”部分匹配,则断言通过并且正则表达式继续。如果不匹配,则断言失败,并且正则表达式返回不匹配。在这种情况下,我们的“...”是“.*[A-Za-z0-9]”,它只是说“检查那里某处是否存在字母数字字符,我们不'不在乎在哪里”。

下一个组件是 [A-Za-z0-9, .!@#$%^&*()_]{3,25} ,仅表示匹配 3 到 25 个字符任何有效的。由于我们的积极前瞻断言,我们已经知道其中至少有一个是字母数字,所以这已经足够了。

You can use a zero-width positive assertion to solve this. Here's the regex, and I'll deconstruct it below.

/(?=.*[A-Za-z0-9])[A-Za-z0-9, .!@#$%^&*()_]{3,25}/

The first component is (?=.*[A-Za-z0-9]). The construct (?=...) is a zero-width assertion, meaning it checks something, but doesn't "eat" any of the output. If the "..." part matches, the assertion passes and the regex continues. If it does not match, the assertion fails, and the regex returns as not matching. In this case, our "..." is ".*[A-Za-z0-9]" which just says "check to see the an alphanumeric character exists in there somewhere, we don't care where".

The next component is [A-Za-z0-9, .!@#$%^&*()_]{3,25} and just says to match between 3 and 25 characters out of any of the valid. We already know that at least one of them is alphanumeric, because of our positive-lookahead assertion, so this is good enough.

三岁铭 2024-12-11 04:02:04

您不能嵌套字符类,但我认为您的意思是

/^([A-Za-z0-9]+[A-Za-z0-9, .!@#$%^&*()_]*){3,25}$/

但这也不起作用,因为量词 {3,25} 也不能嵌套。

试试这个

^(?=.{3,25})[A-Za-z0-9]+[A-Za-z0-9, .!@#$%^&*()_]*$

(?=.{3,25}) 是一个前瞻,只是确保您的长度要求。

You can not nest character classes, but I think what you meant is

/^([A-Za-z0-9]+[A-Za-z0-9, .!@#$%^&*()_]*){3,25}$/

But this will also not work because the quantifier {3,25} can also not be nested.

Try this instead

^(?=.{3,25})[A-Za-z0-9]+[A-Za-z0-9, .!@#$%^&*()_]*$

(?=.{3,25}) is a lookahead that just ensures your length requirement.

摘星┃星的人 2024-12-11 04:02:04

我认为你的正则表达式有点奇怪,你将一个集合包含在一个集合中。

它应该类似于 /^([A-Za-z0-9]+[A-Za-z0-9, .!@#$%^&*()_]*){3,25 }$/ 带括号来定义数字。

I think your regexp is a bit weird, you're enclosing a set within a set.

It should be something like /^([A-Za-z0-9]+[A-Za-z0-9, .!@#$%^&*()_]*){3,25}$/ with parenthesis to define the number.

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