After digging around on stack overflow, I found some code which checks if the string is alphanumeric and longer that 8 chars. It works great. Now how do I make it return true if it contains at least 2 numbers? I think I have to add \d{2}
somewhere.
String pattern = "^[a-zA-Z0-9]*$";
if (s.matches(pattern) && s.length() >= 8){
return true;
}
return false;
You don't need a separate if condition. A single regex will do all for you.
String pattern = "^(?=.*?\\d.*\\d)[a-zA-Z0-9]{8,}$";
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