【问题标题】:Regular expression validate form正则表达式验证表单
【发布时间】:2015-11-25 03:45:49
【问题描述】:

我不知道如何将具有a-zA-Z0-9 并且可以包括$@ 的5 个字符与正则表达式一起输入。这就是我所拥有的

$char_regex = '/^[a-zA-Z0-9@\$]{5}$/';

它一直显示错误。

【问题讨论】:

  • 请为您的表单提供正确和错误输入的示例。
  • 正则表达式对我有用。请提供minimal reproducible example,以便我们重现您的问题

标签: php regex forms validation


【解决方案1】:

使用正向预测

$char_regex = '/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9@\$]{5}$/';

解释:

^                     # from start
(?=.*[a-z])           # means should exist one [a-z] character in some place
(?=.*[A-Z])           # same to upper case letters
(?=.*[0-9])           # same to digits
[a-zA-Z0-9@\$]{5}$    # your current regex

希望对你有帮助。

【讨论】: