I am trying to check whether a password is alphanumeric or not using regex but I am not getting the result I expect. What is the problem with the below code?
boolean passwordOnlyAlphaNumericCheck = false;
Pattern patternAlphaNumericCheck = Pattern.compile("^[a-zA-Z0-9]$");
Matcher matcherAlphaNumericCheck = patternAlphaNumericCheck.matcher(login.getPassword());
if(matcherAlphaNumericCheck.find())
passwordOnlyAlphaNumericCheck = true;
Thanks for help
"^[a-zA-Z0-9]+$"or"^[a-zA-Z0-9]*"- you forgot the quantifier. And you can remove the^and$and use.matches().login.getPassword().matches("[0-9a-zA-Z]*");but what is the difference betweenlogin.getPassword().matches("[0-9a-zA-Z]*");andlogin.getPassword().matches("[0-9a-zA-Z]");