正则表达式以验证密码 [英] Regex to validate password

查看:98
本文介绍了正则表达式以验证密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里一直在寻找一些想法,但我似乎仍在努力提出正则表达式来满足我的要求.

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:

  • 至少1个大写字母
  • 至少1个数字
  • 仅字母数字字符(无特殊字符)
  • 至少8个字符长

我使用的正则表达式是:

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?

推荐答案

将最后一个部分.{8,}更改为[a-zA-Z\d]{8,}

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

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

这篇关于正则表达式以验证密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆