PHP,正则表达式,不允许在密码中包含空格 [英] PHP, Regular Expression, don't allow whitespace in password

查看:711
本文介绍了PHP,正则表达式,不允许在密码中包含空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找问题时间,但是找不到能正常工作的解决方案,或者RegEx产品线有问题.我正在寻找不允许用户在密码中添加空格的部分.密码字段必须要求以下正则表达式列表:

  • 至少1个小写字母[a-z].
  • 至少1个大写字母[A-Z].
  • 至少1个数字字符[0-9].
  • 密码不应包含任何特殊字符,符号或空格.
  • 密码的长度必须至少为8个字符.允许的最大长度为30个字符.

现在,我已经生成了RegEx行,其中包含所有内容,但不包含空格部分.如果有人知道此行有什么问题,请纠正我并告诉我如何解决该问题.顺便说一句,我尝试了[\ S],但没有用,我也不知道为什么.

我的RegExp行:

^(?=(?:.*[A-Z]){1,})(?=(?:.*[a-z]){1,})(?=(?:.*\d){1,})(?!(?:.*[!@#$%^&*()\-_=+{};:,<.>]))(.{8,30})$

以下是此RegEx的有效和无效密码的完整演示:演示

感谢所有能回答并帮助我的人.

解决方案

我不知道您如何尝试使用\S,但这正是您可以利用和使用的方法,而不是主要使用模式中的.:

\A                 # Start of string
  (?=[^A-Z]*[A-Z]) # Require at least one uppercase letter 
  (?=[^a-z]*[a-z]) # Require at least one lowercase letter
  (?=\D*\d)        # Require at least one digit
  (?![^!@#$%^&*()\-_=+{};:,<.>]*[!@#$%^&*()\-_=+{};:,<.>]) # No special chars
 \S{8,30}          # Match 8 to 30 non-whitespace chars
\z                 # Unambiguous end of string.

请参见 regex演示(演示regex有所不同,因为测试针对多行字符串,这是您应该使用的字符串.)

在这里,我使用了先行内对比的原理,并删除了不必要的非捕获组.

此外,使用\z锚点匹配字符串的结尾更加安全,因为$可以在字符串的最后换行符之前匹配.

I was looking for my problem hours and hours but can't find a solution that works fine, or maybe something it's wrong with my RegEx line. I'm looking for part where user won't be allowed to put whitespace in the password. The password field must require this list of regular expressions:

  • A minimum of 1 lower case letter [a-z].
  • A minimum of 1 upper case letter [A-Z].
  • A minimum of 1 numeric character [0-9].
  • Password should not contain any special characters, symbols or spaces.
  • Passwords must be at least 8 characters in length. Maximum allowed length is 30 characters.

Right now I have generated RegEx line that include everything, but not the part with the whitespaces. If anyone know what is wrong with this line, please correct me and tell me how to fix the problem. Btw, I tried [\S] but doesn't work, I don't know why.

My RegExp line:

^(?=(?:.*[A-Z]){1,})(?=(?:.*[a-z]){1,})(?=(?:.*\d){1,})(?!(?:.*[!@#$%^&*()\-_=+{};:,<.>]))(.{8,30})$

Here is full demo of valid and non-valid passwords with this RegEx: Demo

Thanks to all that will answer and help me.

解决方案

I do not know how you tried using \S, but this is exactly what you can leverage and use instead of . in the main consuming pattern:

\A                 # Start of string
  (?=[^A-Z]*[A-Z]) # Require at least one uppercase letter 
  (?=[^a-z]*[a-z]) # Require at least one lowercase letter
  (?=\D*\d)        # Require at least one digit
  (?![^!@#$%^&*()\-_=+{};:,<.>]*[!@#$%^&*()\-_=+{};:,<.>]) # No special chars
 \S{8,30}          # Match 8 to 30 non-whitespace chars
\z                 # Unambiguous end of string.

See the regex demo (the demo regex is a bit different since the test is performed against a multiline string, here is the one you should use.)

Here, I used the principle of contrast inside the lookaheads and removed unnecessary non-capturing groups.

Also, it is safer to use \z anchor to match the end of string since $ can match before the final newline in the string.

这篇关于PHP,正则表达式,不允许在密码中包含空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆