Validating Password using Regex

后端 未结 1 846
死守一世寂寞
死守一世寂寞 2020-12-13 15:45

I am working on a Rails 3 application that needs to validate the password based on the following criteria: must be at least 6 characters and include one number and one

相关标签:
1条回答
  • 2020-12-13 16:11

    Use lookahead assertions:

    /^(?=.*[a-zA-Z])(?=.*[0-9]).{6,}$/
      |             |          |
      |             |          |
      |             |          Ensure there are at least 6 characters.
      |             |
      |             Look ahead for an arbitrary string followed by a number.
      |                        
      Look ahead for an arbitrary string followed by a letter.
    

    Technically in this case you don't need the anchors, but it's good habit to use them.

    0 讨论(0)
提交回复
热议问题