protected override ValidationResult IsValid(object value, ValidationContext validationContext) { string password = (string)value; // password must be between 8 and 30 characters. if (password == null) { return(new ValidationResult("Your password cannot be empty and is required")); } var passwordMinLength = 8; var passwordMaxLength = 30; if (password.Length < passwordMinLength || password.Length > passwordMaxLength) { return(new ValidationResult($"Your password must be at least {passwordMinLength}, and no more than {passwordMaxLength} characters.")); } // password must have at least 1 upper case character. const string BaseString = "Your password must contain at least 1 {0} character."; if (!password.Any(char.IsUpper)) { return(new ValidationResult(String.Format(BaseString, "upper case"))); } // password must have at least 1 lower case character. if (!password.Any(char.IsLower)) { return(new ValidationResult(String.Format(BaseString, "lower case"))); } // password must have at least 1 digit. if (!password.Any(char.IsDigit)) { return(new ValidationResult(String.Format(BaseString, "digit"))); } // password must have at least 1 special character. const string Special_Char_String = "!@#$%^&*?`~|"; if (!password.Any(c => Special_Char_String.Contains(c))) { return(new ValidationResult(String.Format(BaseString, "special") + $" Valid Special characters are '{Special_Char_String}'.")); } // password cannot contain any whitespace. if (password.Any(char.IsWhiteSpace)) { return(new ValidationResult("Your password cannot contain any spaces, tabs, or newlines.")); } return(ValidationResult.Success); }
public static string CleanString2(string original, int size = 31, string specialchars = "") { StringBuilder sbuilder = new StringBuilder(); string Special_Char_String = ",.=()!@#$%&~`'+ ^*[]{}/?:;<>|"; // 11-28-2012: removed "-" from list if (specialchars != "") { Special_Char_String += specialchars; } foreach (char c in original) { if (!Special_Char_String.Contains(c)) { sbuilder.Append(c); } else { if (c == '<') { sbuilder.Append("<"); } else if (c == '>') { sbuilder.Append(">"); } else { sbuilder.Append("_"); } } } if (sbuilder.Length > size) { sbuilder.Remove(size - 1, sbuilder.Length - size); } return(sbuilder.ToString()); }