-1

I need some help getting my REGEX to work.

I need to validate a password that is based on the following rules:

At least 1 uppercase char at least 1 lowercase char At least 1 number min length 8 char.

0 or more special character no white space

This what I have so far

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

How do I prevent whitespace?

Thanks for your help

user2570135
  • 2,669
  • 6
  • 50
  • 80
  • 1
    Avoid trying to force RegEx into a role it doesn't like; I'd just do this with a simple iteration over the characters and keeping track of your requirements. – xtratic Jul 06 '20 at 19:34
  • Does this answer your question? [PHP, Regular Expression, don't allow whitespace in password](https://stackoverflow.com/questions/37735834/php-regular-expression-dont-allow-whitespace-in-password) – Ryszard Czech Jul 06 '20 at 20:18
  • See [Reference - Password Validation](https://stackoverflow.com/q/48345922/3600709) – ctwheels Jul 06 '20 at 21:53
  • @ctwheels why a reference on the world of passwords and encription ? Is it really going to change anybody's mind on a regex they want to use ? –  Jul 06 '20 at 21:57
  • @Maxt8r if the user or anyone viewing this question is concerned about security, then it really might be useful. Nothing wrong with presenting a user with more information. This question is related to regex and passwords. Current security guidelines tell us regex is not the best tool for the job. Similarly, if a question is posted here asking how to parse HTML with regex, a user might be told to use a parser. Use the best tool for the job. Regex isn't a *do-everything-tool*. Can you hammer in a nail with an exacto knife? Yes you can, but why would you? – ctwheels Jul 06 '20 at 22:02
  • @ctwheels you are correct. Yes it's very informative, I plan on going back to read the whole thing when I have some spare time. I like Amazon's password philosophy, it's well.. amaz (on) ing. –  Jul 06 '20 at 22:09
  • @Maxt8r also, at the end of the reference I linked to, there's a regex (in case anyone *has* to use regex) that allows to check for one uppercase, lowercase, number, symbol. It does allow *some* whitespace, but whatever isn't a control character. The nice thing is that is also allows non-ASCII characters (a user can use `é` in their password for example) - think international users; French language uses accents, Arabic languages, Russia and China use entirely different characters. Don't forget about those users and their language-specific keyboards. Regex can be used, but more thought needed. – ctwheels Jul 06 '20 at 22:17

1 Answers1

0

Just replace the dot with \S (not a space):

String pattern = r'^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])\S{8,}$';
Toto
  • 89,455
  • 62
  • 89
  • 125