I need my code to check the password and one of the letters in password should be in upper case. When user is entering. Please assist.
int digcheck=0,charcheck=0,symbcheck=0;
for (int i = 0; i < passwordraw.length(); i++) {
if (Character.isDigit(passwordraw.charAt(i)))
{
digcheck++;
}
else if(Character.isLetter(passwordraw.charAt(i)))
{
charcheck++;
}
else
{
symbcheck++;
}
}
if(digcheck<3)
{
digcheck=0;
throw new OBSSecurityException("INVALID PASSWORD! Must have atleast three(3) digits.");
}else if(charcheck<5)
{
charcheck=0;
throw new OBSSecurityException("INVALID PASSWORD! Must have atleast five(5) alpha.");
}
else if(symbcheck<1)
{
symbcheck=0;
throw new OBSSecurityException("INVALID PASSWORD! Must have atleast one(1) symbol.");
}
use regex.
make the password match with .*[AZ]+.*
this will make your password have at least one uppercase char
[EDIT] you can also use regex to find the other restrictions.
to have at least 3 digits you can use this regex
.*[0-9]{1}.*[0-9]{1}.*[0-9]{1}.*
this will match anything plus 1 digit, more anything, at least one digit, and so on.. you can have the similar regex to other validations, please net us know if you need help
Don't use nested else-if
as your conditions are different. If a condition is true than other two will be skipped.
if(digcheck<3){
digcheck=0;
throw new OBSSecurityException("INVALID PASSWORD! Must have atleast three(3) digits.");
}
if(charcheck<5){
charcheck=0;
throw new OBSSecurityException("INVALID PASSWORD! Must have atleast five(5) alpha.");
}
if(symbcheck<1){
symbcheck=0;
throw new OBSSecurityException("INVALID PASSWORD! Must have atleast one(1) symbol.");
}
Write a method that checks if password contains at least one upper case character:
public boolean containsAtLeastOneUpperCase(String password) {
for(Character c : password.toCharArray()) {
if (Character.isUpperCase(c)) {
return true;
}
}
return false;
}
int digcheck=0,charcheck=0,symbcheck=0,caseCheck=0;
for (int i = 0; i < passwordraw.length(); i++) {
if (Character.isDigit(passwordraw.charAt(i)))
{
digcheck++;
}
else if(Character.isLetter(passwordraw.charAt(i)))
{
charcheck++;
if(Character.isUpperCase(passwordraw.charAt(i))){
caseCheck++;
}
}
else
{
symbcheck++;
}
}
if(digcheck<3)
{
digcheck=0;
throw new OBSSecurityException("INVALID PASSWORD! Must have atleast three(3) digits.");
}else if(charcheck<5)
{
charcheck=0;
throw new OBSSecurityException("INVALID PASSWORD! Must have atleast five(5) alpha.");
}
else if(symbcheck<1)
{
symbcheck=0;
throw new OBSSecurityException("INVALID PASSWORD! Must have atleast one(1) symbol.");
}
else if(caseCheck<1)
{
caseCheck=0;
throw new OBSSecurityException("INVALID PASSWORD! Must have atleast one(1) Uppercase Letter.");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.