Hey all ,,,
i'm working on a multi language website and i want to localize the validation error messages for most of the ValidationAttribute such as [Requried]
I know it can be done as Phil Haack have shown in this article.
[Required(ErrorMessageResourceType = typeof(Resources),
ErrorMessageResourceName = "Required")]
but i want to to customize the error message the way i did with my custom Validation Attributes in here :
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property,
AllowMultiple = false,
Inherited = true)]
public sealed class ValidateminRequiredNonalphanumericCharactersAttribute
: ValidationAttribute
{
private const string _defaultErrorMessage = // Message From Resource Here ( i will be using two variables in this message )
private readonly int _minnonalphanumericCharactersCounter = Membership.Provider.MinRequiredNonAlphanumericCharacters;
public ValidateminRequiredNonalphanumericCharactersAttribute()
: base(_defaultErrorMessage)
{
}
public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentUICulture,
ErrorMessageString, name, _minnonalphanumericCharactersCounter);
}
public override bool IsValid(object value)
{
string valueAsString = value as string;
if (String.IsNullOrEmpty(valueAsString))
return false;
int nonalphanumericCharactersCounter = 0;
char[] password = valueAsString.ToCharArray();
foreach (char c in password)
{
if (!char.IsNumber(c) && !char.IsLetter(c))
nonalphanumericCharactersCounter++;
}
return (nonalphanumericCharactersCounter >= _minnonalphanumericCharactersCounter);
}
}
any idea ?