Created
November 27, 2023 10:45
-
-
Save rakneo/0c874e1f67a30b5f250d1cb9034a6a14 to your computer and use it in GitHub Desktop.
password validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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