Reputation: 954
As far as I know the System.ComponentModel.DataAnnotations.DataTypeAttribute not works in model validation in MVC v1. For example,
public class Model
{
[DataType("EmailAddress")]
public string Email {get; set;}
}
In the codes above, the Email property will not be validated in MVC v1. Is it working in MVC v2?
Upvotes: 21
Views: 13388
Reputation: 561
Starting from .NET 4.5 there is EmailAddressAttribute, which has a correct implementation of the IsValid
method. So if you're targeting .NET 4.5, then for validation please consider using the EmailAddressAttribute
instead of a custom one. For example,
public class Model
{
[EmailAddress(ErrorMessage = "INVALID EMAIL")]
public string Email {get; set;}
}
And if you're curious about the implementation of EmailAddressAttribute
, then here is the decompiled (using JetBrains dotPeek decompiler) source of the class:
using System;
using System.ComponentModel.DataAnnotations.Resources;
using System.Text.RegularExpressions;
namespace System.ComponentModel.DataAnnotations
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public sealed class EmailAddressAttribute : DataTypeAttribute
{
private static Regex _regex = new Regex("^((([a-z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])+(\\.([a-z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])+)*)|((\\x22)((((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(([\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(\\\\([\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF]))))*(((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(\\x22)))@((([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))\\.)+(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))\\.?$", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.Compiled);
static EmailAddressAttribute()
{
}
public EmailAddressAttribute()
: base(DataType.EmailAddress)
{
this.ErrorMessage = DataAnnotationsResources.EmailAddressAttribute_Invalid;
}
public override bool IsValid(object value)
{
if (value == null)
return true;
string input = value as string;
if (input != null)
return EmailAddressAttribute._regex.Match(input).Length > 0;
else
return false;
}
}
}
Upvotes: 2
Reputation: 31882
[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.Compiled);
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));
}
}
Upvotes: 35
Reputation: 1106
Alternatively, you can directly use RegularExpression attribute on your field instead of creating your own attribute that is in the end going to check for a regex matching.
[RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = PaErrorMessages.InvalidEmailAddress)]
public string Email { get; set; }
Upvotes: 9
Reputation: 998
Check out Scott Guthrie's blog post on MVC 2 validation. It is excellent. http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx
Upvotes: 1
Reputation: 16661
Like LukLed pointed out, DataTypeAttribute doesn't do any validation by default. But it does influence templates regarding how the data is presented.
For example if you call Html.DisplayFor()
method on a model that has DataType(DataType.EmailAddress)
attribute, it'll format its value with <a href="mailto:{0}">{0}</a>
(at least in MVC RC2).
Upvotes: 13