[DataType("EmailAddress")]
doesn't influence validation by default. This is IsValid
method of this attribute (from reflector):
public override bool IsValid(object value)
{
return true;
}
This is example of custom DataTypeAttribute to validate Emails (taken from this site http://davidhayden.com/blog/dave/archive/2009/08/12/CustomDataTypeAttributeValidationCustomDisplay.aspx):
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public class EmailAddressAttribute : DataTypeAttribute
{
private readonly Regex regex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.Compile
public EmailAddressAttribute() : base(DataType.EmailAddress)
{
}
public override bool IsValid(object value)
{
string str = Convert.ToString(value, CultureInfo.CurrentCulture);
if (string.IsNullOrEmpty(str))
return true;
Match match = regex.Match(str);
return ((match.Success && (match.Index == 0)) && (match.Length == str.Length));
}
}