Java – Alphanumeric regular expression
•
Java
I want a regular expression in Java, which must contain at least one letter and one number in any position The password contains numbers and numbers
This applies to:
"1a1b23nh" accepted
"Bc112w" accepted
"ABC" is not acceptable
"123" not accepted
Special characters are not allowed
Solution
(([a-z]+[0-9]+)+|(([0-9]+[a-z]+)+))[0-9a-z]*
(([a-z]+[0-9]+)+|(([0-9]+[a-z]+)+))[0-9a-z]*
How to do simple content checking? Check for numbers and characters
String input = "b45z4d";
boolean alpha = false;
boolean numeric = false;
boolean accepted = true;
for (int i = 0; i < input.length(); ++i)
{
char c = input.charAt(i);
if (Character.isDigit(c))
{
numeric = true;
} else if (Character.isLetter(c))
{
alpha = true;
} else
{
accepted = false;
break;
}
}
if (accepted && alpha && numeric)
{
// Then it is correct
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
