I've been Googling a lot and I didn't find an answer for my question:
How can I check with a regular expression whether a string contains at least one of each of these:
~`!@#$%^&*()-_=+\|[{]};:'",<.>/?
So I need at least a capital letter and at least a small letter and at least a digit and at least a special character.
I'm sure the answer is very simple, but I can't find it. Any help is greatly appreciated.
To check if a letter in a string is uppercase or lowercase use the toUpperCase() method to convert the letter to uppercase and compare it to itself. If the comparison returns true , then the letter is uppercase, otherwise it's lowercase. Copied!
To check if a string contains special characters, call the test() method on a regular expression that matches any special character. The test method will return true if the string contains at least 1 special character and false otherwise.
The java. lang. Character. isUpperCase(char ch) determines if the specified character is an uppercase character.
Regular expressions aren't very good for tests where you require all of several conditions to be met.
The simplest answer therefore is not to try and test them all at the same time, but just to try each of the four classes in turn.
Your code might be fractionally slower, but it'll be easier to read and maintain, e.g.
public boolean isLegalPassword(String pass) { if (!pass.matches(".*[A-Z].*")) return false; if (!pass.matches(".*[a-z].*")) return false; if (!pass.matches(".*\\d.*")) return false; if (!pass.matches(".*[~!.......].*")) return false; return true; }
EDIT fixed quote marks - been doing too much damned JS programming...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With