Skip to content

Instantly share code, notes, and snippets.

@rakneo
Created November 27, 2023 10:45
Show Gist options
  • Save rakneo/0c874e1f67a30b5f250d1cb9034a6a14 to your computer and use it in GitHub Desktop.
Save rakneo/0c874e1f67a30b5f250d1cb9034a6a14 to your computer and use it in GitHub Desktop.
password validation
bool validatePassword(String password) {
// Check if the password has exactly 6 characters
if (password.length != 6) {
return false;
}
// Check if the password contains at least one uppercase letter
bool hasUppercase = false;
for (int i = 0; i < password.length; i++) {
if (password[i] == password[i].toUpperCase() && password[i] != password[i].toLowerCase()) {
hasUppercase = true;
break;
}
}
// Check if the password contains at least one lowercase letter
bool hasLowercase = false;
for (int i = 0; i < password.length; i++) {
if (password[i] == password[i].toLowerCase() && password[i] != password[i].toUpperCase()) {
hasLowercase = true;
break;
}
}
// Return true only if all conditions are met
return hasUppercase && hasLowercase;
}
void main() {
// Example usage:
String passwordToCheck = "AbCdEf";
if (validatePassword(passwordToCheck)) {
print("Password is valid!");
} else {
print("Password is invalid.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment