开发者

Regex breakdown

Can someone help me with this regex password validation for rails?

It currently is set to ensure:

  • Password should contain at least one integer.
  • Password should contain at least one alphabet(either i开发者_开发问答n lowercase or uppercase).
  • Password can have special characters from 20 to 7E ascii values.
  • Password should be minimum of 8 and maximum of 40 characters long.

How can I remove the one single decimal digit rule?

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


I'm going to recommend against a monolithic regex to test all of your password requirements. It seems much easier to write them as lots of small statements:

if(password.length >= 8 &&
   password.length <= 40 &&
   password.contains('\d') &&
   password.contains('[a-zA-Z]') &&
   password.contains('^[\x20-\x7E]$'))
   return true
else
   return false

Its a bit more self-documenting to future maintainers, and a lot easier on the eyes.


\d represents all number digits. You want:

/^(?=.*([a-z]|[A-Z]))([\x20-\x7E]){8,40}$/


/^(?=.*([a-z]|[A-Z]))([\x20-\x7E]){8,40}$/
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜