Regex to validate password

一世执手 提交于 2019-12-02 01:08:25

问题


I've looked on here for some ideas but I still seem to be struggling with coming up with a regular expression to meet my requirements.

I need a regular expression to check a password format, the criteria are:

  • At least 1 uppercase letter
  • At least 1 number
  • Only alphanumeric characters (no special characters)
  • At least 8 characters long

The regular expression I'm using is:

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$

However this is also allowing characters like !$&.

Is there a modification I need to make to this to get it to stop these special characters being accepted?


回答1:


Change the last part .{8,} to [a-zA-Z\d]{8,}

 ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$


来源:https://stackoverflow.com/questions/9698816/regex-to-validate-password

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!