开发者

Regular expression to exclude special characters

开发者 https://www.devze.com 2023-01-07 17:51 出处:网络
I need a regex for a password which meets following constraints in my rails project: have a minimum of 8 and a maximum of 16 characters

I need a regex for a password which meets following constraints in my rails project:

  • have a minimum of 8 and a maximum of 16 characters
  • be alphanumeric only
  • contain at least one letter and one number.

My current regex is:

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

This allows me all the restrictions but the speci开发者_JS百科al characters part is not working. What is it that I am doing wrong. Can someone please correct this regex?

Thanks in advance.


/^(?=.*\d)(?=.*[a-zA-Z])[0-9a-zA-Z]{8,16}$/

The last part of your regex, .{8,16}, allows any character with a dot.

The lookahead only makes sure that there's at least one digit and one letter - it doesn't say anything about other characters. Also, note that I've updated your letter matching part - you don't need two character classes.

Disallowing special characters in a password is totally counter intuitive. Why are you doing that?

0

精彩评论

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