Priyank
Priyank

Reputation: 14387

Regular expression to exclude special characters

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

My current regex is:

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

This allows me all the restrictions but the special characters part is not working. What is it that I am doing wrong. Can someone please correct this regex?

Thanks in advance.

Upvotes: 0

Views: 2648

Answers (1)

Amarghosh
Amarghosh

Reputation: 59451

/^(?=.*\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?

Upvotes: 3

Related Questions