Password Validator
public class PasswordValidation
{
public static bool ValidatePassword(string password)
{
if (string.IsNullOrEmpty(password)) return false;
return PasswordValidator.IsValidPassword(password.Trim());
}
public static class PasswordValidator
{
public static bool IsValidPassword(string password)
{
const int MIN_LENGTH = 8;
const int MAX_LENGTH = 15;
if (password == null) throw new ArgumentNullException();
bool meetsLengthRequirements = password.Length >= MIN_LENGTH && password.Length <= MAX_LENGTH;
bool hasUpperCaseLetter = false;
bool hasLowerCaseLetter = false;
bool hasDecimalDigit = false;
bool hasSpecialChar = false;
bool startedWithLetter = false;
if (meetsLengthRequirements)
{
bool firstchar = true;
foreach (char c in password)
{
if (firstchar)
{
startedWithLetter = char.IsLetter(c);
firstchar = false;
}
if (startedWithLetter)
{
if (char.IsUpper(c)) hasUpperCaseLetter = true;
else if (char.IsLower(c)) hasLowerCaseLetter = true;
else if (char.IsDigit(c)) hasDecimalDigit = true;
else if (!char.IsLetterOrDigit(c)) hasSpecialChar = true;
}
}
}
bool isValid = meetsLengthRequirements
&& hasUpperCaseLetter
&& hasLowerCaseLetter
&& hasDecimalDigit
&& startedWithLetter
&& hasSpecialChar;
return isValid;
}
}
}
--------------------------------
To Use
----------------------------------
else if (string.IsNullOrEmpty(Password))
{
UserDialogs.Instance.AlertAsync("The Field Cannot be Empty", "Alert", "Re-Try");
}
else if (!EmailValidator.ValidateEmailAddress(Password))
{
UserDialogs.Instance.AlertAsync("Invalid Pasword Format. Use a combination of upper case, lower case, numbers and special characters", "Alert", "Re-Try");
}
{
public static bool ValidatePassword(string password)
{
if (string.IsNullOrEmpty(password)) return false;
return PasswordValidator.IsValidPassword(password.Trim());
}
public static class PasswordValidator
{
public static bool IsValidPassword(string password)
{
const int MIN_LENGTH = 8;
const int MAX_LENGTH = 15;
if (password == null) throw new ArgumentNullException();
bool meetsLengthRequirements = password.Length >= MIN_LENGTH && password.Length <= MAX_LENGTH;
bool hasUpperCaseLetter = false;
bool hasLowerCaseLetter = false;
bool hasDecimalDigit = false;
bool hasSpecialChar = false;
bool startedWithLetter = false;
if (meetsLengthRequirements)
{
bool firstchar = true;
foreach (char c in password)
{
if (firstchar)
{
startedWithLetter = char.IsLetter(c);
firstchar = false;
}
if (startedWithLetter)
{
if (char.IsUpper(c)) hasUpperCaseLetter = true;
else if (char.IsLower(c)) hasLowerCaseLetter = true;
else if (char.IsDigit(c)) hasDecimalDigit = true;
else if (!char.IsLetterOrDigit(c)) hasSpecialChar = true;
}
}
}
bool isValid = meetsLengthRequirements
&& hasUpperCaseLetter
&& hasLowerCaseLetter
&& hasDecimalDigit
&& startedWithLetter
&& hasSpecialChar;
return isValid;
}
}
}
--------------------------------
To Use
----------------------------------
else if (string.IsNullOrEmpty(Password))
{
UserDialogs.Instance.AlertAsync("The Field Cannot be Empty", "Alert", "Re-Try");
}
else if (!EmailValidator.ValidateEmailAddress(Password))
{
UserDialogs.Instance.AlertAsync("Invalid Pasword Format. Use a combination of upper case, lower case, numbers and special characters", "Alert", "Re-Try");
}
Comments
Post a Comment