微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Validate.js ReGex 未按预期工作

如何解决Validate.js ReGex 未按预期工作

我正在尝试使用格式:模式:Validate.js 的功能来确保密码满足至少一个大写、一个小写、一个数字和一个特殊字符的要求,并且具有最小长度8 个,最多 20 个。存在和长度功能如预期。有人有任何提示吗?

 password: {
    presence: {
      allowEmpty: false,message: "is required",admin: "required",label: "Password*",},length: {
      minimum: 8,message: "must be at least 8 characters",maximum: 20,format: {
      pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!|@|#|\$|%|\^|&|\*])(?=.{8,20})/,}
  },

解决方法

使用

/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*]).{8,20}$/

说明

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    [a-z]                    any character of: 'a' to 'z'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    [A-Z]                    any character of: 'A' to 'Z'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    [0-9]                    any character of: '0' to '9'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    [!@#$%^&*]               any character of: '!','@','#','$','%','^','&','*'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  .{8,20}                  any character except \n (between 8 and 20
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  $                        before an optional \n,and the end of the
                           string

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。