dsnm64969 2017-09-30 04:00
浏览 44
已采纳

PHP正则表达式代码“不匹配一次”

Working on code similar to this previous post but for passwords ($email is now $password). Trying have validation check to see if password field has at least one number, one lowercase letter, one uppercase letter, and one of the symbols !@#$%^&*

PHP-side validation uses regex

/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*])[a-zA-Z\d!@#$%^&*]{6,20}$/

which works fine. However for js validation I'm using this for "if password does not contain one of each"

if (preg_match('/([^a-z]{1})([^A-Z]{1})(^\d{1})([^!@#$%^&*]{1})+/', $password, $matches)) {
echo 'Password must contain _, _, _';

Total beginner and very confused.

  • 写回答

1条回答 默认 最新

  • dongyouji7022 2017-09-30 04:22
    关注

    Why don't you use the same regex pattern in js too??

    /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*])[a-zA-Z\d!@#$%^&*]{6,20}$/
    

    This would work fine in js as well. If only you need to see if password does not contain one of each use a negation as

    if (!preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*])[a-zA-Z\d!@#$%^&*]{6,20}$/', $password, $matches)) {
    echo 'Password must contain _, _, _';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源