My RegisterModel
has the Password
Property as below:
public class RegisterModel
{
[Required]
[StringLength(100, ErrorMe
Though you have changed it in your RegisterModel
, notice that on your Register View
, the value is coming from Membership
class that is configured according to your Membership
Provider
in your web.config
file.
So, Check your web.config
file. It has the following code:
Change minRequiredPasswordLength = "8"
here too and it will work for you.
OR
If you don't want to make changes in your Membership Provider
, then you can still do this by writing your own Custom Attribute for MinPasswordLength
as below:
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter , AllowMultiple = false, Inherited = true)]
public sealed class MinRequiredPasswordLengthAttribute : ValidationAttribute, IClientValidatable
{
private readonly int _minimumLength = Membership.MinRequiredPasswordLength;
public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentCulture, ErrorMessageString, name, _minimumLength);
}
public override bool IsValid(object value)
{
string password = value.ToString();
return password.Length >= this._minimumLength;
}
public IEnumerable GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
return new[]{ new ModelClientValidationStringLengthRule(FormatErrorMessage(metadata.GetDisplayName()), _minimumLength, int.MaxValue)
};
}
}
Then update your RegisterModel
to use the MinRequiredPasswordLength
DataAnnotation instead.
[Required]
[MinRequiredPasswordLength(ErrorMessage = "The {0} must be at least {1} character(s) long.")]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }