Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
C# program to check password validity
While creating a password, you may have seen validation requirements on websites that ensure a password is strong and secure. Common password requirements include −
- Minimum 8 characters and maximum 14 characters
- At least one lowercase letter
- No whitespace characters
- At least one uppercase letter
- At least one special character
Let us create a complete password validation program that checks all these conditions systematically.
Complete Password Validation Program
using System;
using System.Linq;
class PasswordValidator {
public static bool IsValidPassword(string passwd) {
// Check length (8-14 characters)
if (passwd.Length 14) {
Console.WriteLine("Password must be between 8 and 14 characters.");
return false;
}
// Check for at least one lowercase letter
if (!passwd.Any(char.IsLower)) {
Console.WriteLine("Password must contain at least one lowercase letter.");
return false;
}
// Check for no whitespace
if (passwd.Contains(" ")) {
Console.WriteLine("Password must not contain whitespace.");
return false;
}
// Check for at least one uppercase letter
if (!passwd.Any(char.IsUpper)) {
Console.WriteLine("Password must contain at least one uppercase letter.");
return false;
}
// Check for at least one special character
string specialChars = @"!@#$%^&*()_+-=[]{}|;:,.?";
if (!passwd.Any(ch => specialChars.Contains(ch))) {
Console.WriteLine("Password must contain at least one special character.");
return false;
}
return true;
}
public static void Main(string[] args) {
string[] passwords = {
"MyPass123!", // Valid
"short7!A", // Too short
"toolongpassword123!A", // Too long
"nouppercasehere123!", // No uppercase
"NOLOWERCASE123!", // No lowercase
"MyPassword123", // No special character
"My Pass123!" // Contains whitespace
};
foreach (string password in passwords) {
Console.WriteLine($"\nTesting password: '{password}'");
if (IsValidPassword(password)) {
Console.WriteLine("? Password is valid!");
} else {
Console.WriteLine("? Password is invalid.");
}
}
}
}
The output of the above code is −
Testing password: 'MyPass123!' ? Password is valid! Testing password: 'short7!A' Password must be between 8 and 14 characters. ? Password is invalid. Testing password: 'toolongpassword123!A' Password must be between 8 and 14 characters. ? Password is invalid. Testing password: 'nouppercasehere123!' Password must contain at least one uppercase letter. ? Password is invalid. Testing password: 'NOLOWERCASE123!' Password must contain at least one lowercase letter. ? Password is invalid. Testing password: 'MyPassword123' Password must contain at least one special character. ? Password is invalid. Testing password: 'My Pass123!' Password must not contain whitespace. ? Password is invalid.
Individual Validation Methods
Let's examine each validation rule separately −
Length Validation (8-14 Characters)
if (passwd.Length 14)
return false;
Lowercase Letter Check
if (!passwd.Any(char.IsLower))
return false;
Whitespace Validation
if (passwd.Contains(" "))
return false;
Uppercase Letter Check
if (!passwd.Any(char.IsUpper))
return false;
Special Character Validation
string specialChars = @"!@#$%^&*()_+-=[]{}|;:,.?";
if (!passwd.Any(ch => specialChars.Contains(ch)))
return false;
Enhanced Password Validator with Scoring
using System;
using System.Linq;
class AdvancedPasswordValidator {
public static int CalculatePasswordScore(string passwd) {
int score = 0;
// Length scoring
if (passwd.Length >= 8) score += 2;
if (passwd.Length >= 12) score += 1;
// Character type scoring
if (passwd.Any(char.IsLower)) score += 1;
if (passwd.Any(char.IsUpper)) score += 1;
if (passwd.Any(char.IsDigit)) score += 1;
string specialChars = @"!@#$%^&*()_+-=[]{}|;:,.?";
if (passwd.Any(ch => specialChars.Contains(ch))) score += 2;
// Deduct points for common weaknesses
if (passwd.Contains(" ")) score -= 2;
return Math.Max(0, score);
}
public static string GetPasswordStrength(int score) {
if (score >= 7) return "Very Strong";
if (score >= 5) return "Strong";
if (score >= 3) return "Medium";
if (score >= 1) return "Weak";
return "Very Weak";
}
public static void Main(string[] args) {
string[] testPasswords = {
"MySecure123!",
"password",
"PASSWORD123",
"MyP@ss1"
};
foreach (string pwd in testPasswords) {
int score = CalculatePasswordScore(pwd);
string strength = GetPasswordStrength(score);
Console.WriteLine($"Password: '{pwd}' | Score: {score} | Strength: {strength}");
}
}
}
The output of the above code is −
Password: 'MySecure123!' | Score: 7 | Strength: Very Strong Password: 'password' | Score: 3 | Strength: Medium Password: 'PASSWORD123' | Score: 4 | Strength: Strong Password: 'MyP@ss1' | Score: 5 | Strength: Strong
Conclusion
Password validation in C# involves checking multiple criteria such as length, character types, and forbidden characters. Using LINQ methods like Any() and Contains() makes the validation process efficient and readable, ensuring passwords meet security requirements.
Advertisements
