Mathew Thompson
Mathew Thompson

Reputation: 56429

Password Regex Validation: Preventing Spaces

I'm trying to adhere to the following password rule:

Must be 6 to 15 characters, include at least one lowercase letter, one uppercase letter and at least one number. It should also contain no spaces.

Now, for everything but the spaces, I've got:

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{6,15}$

Problem is, that allows spaces.

After looking around, I've tried using \s, but that messes up my lowercase and uppercase requirements. I also seen another suggestion to replace the * with a +, but that seemed to break the entire thing.

To clarify, this is a client requirement unfortunately, I'm never usually this strict with passwords.

Upvotes: 5

Views: 3755

Answers (1)

Toto
Toto

Reputation: 91428

How about:

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)\S{6,15}$

\S stands for any NON space character.

Upvotes: 9

Related Questions