简体   繁体   English

正则表达式或 if 循环 .... 用于密码验证

[英]Regex or if loop ....for password validation

I want to validate password in which it should accept我想验证它应该接受的密码

  • At least 2 numerical,至少2个数字,
  • At least 1 upper-case,至少 1 个大写,
  • At least 1 lower-case,至少 1 个小写,
  • At least 1 symbol.至少 1 个符号。
  • And no space.而且没有空间。
  • Minimum length of password should be 8 characters密码的最小长度应为 8 个字符

Like shown below如下图所示

  • asdA1@#2. asdA1@#2。
  • 1Aas&3%A 1Aas&3%A
  • 21asdA@! 21asdA@!

Numerical symbols not necessarily have to be together.数字符号不一定必须在一起。

It can be anywhere, and separated.它可以在任何地方,并且可以分开。

you can use the regex你可以使用正则表达式

(?=.*\d.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[&@%#!])[^\s]{8,}

see the regex101 demoregex101 演示

I would split all the conditions to seperated boolean methods, and one validatePassword method that will call all the condition methods, without regex.我会将所有条件拆分为单独的布尔方法,以及一个将调用所有条件方法的 validatePassword 方法,而无需正则表达式。 code should be readable and easy to maintenance.代码应该可读且易于维护。

Tommorow you might want to change something in the conditions, it will be hard to change the regex and you will have to test all the schenarios over again.明天您可能想要更改条件中的某些内容,更改正则表达式将很困难,您将不得不再次测试所有方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM