开发者

Regex for password verification

开发者 https://www.devze.com 2023-02-15 09:32 出处:网络
I want to validate password .The following are my requirements. Minimum password length:8 Minimum number of lower case characters:1

I want to validate password .The following are my requirements.

Minimum password length: 8

Minimum number of lower case characters: 1

Minimum number of upper case characters: 1

Minimu开发者_如何学编程m number of numeric characters: 1

How to write a regex for this ?


You can use the following regex:

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


I agree with @Russell, a function is a better choice for password validation. And it's hard to imagine a single Regex handling all these cases. I think you would have to check each one in turn.

Individually, the Regex expressions are:

  • .{8} matches at least 8 characters
  • [a-z] matches a single lowercase character
  • [A-Z] matches a single uppercase character
  • [0-9] matches a digit

That having been said, these would only be useful for client-side checking prior to having the server do in-depth validation.


Please find below regex for your requirements:

(?=^.{8}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$ 
0

精彩评论

暂无评论...
验证码 换一张
取 消