3

I am using MVC 3.

I have a text area in which user can enter multiple emails addresses. Emails can be separated by a comma and a space. User may hit enter in the box as well.

  1. Is there an attribute that can handle this scenario?

  2. I am using regular expression to check for the characters and it is failing for "abc@abc.com, tyz@tyz.com"

Here is my regular expression: [RegularExpression(@"([a-zA-Z0-9 .@-_\n\t\r]+)", ErrorMessage = ValidationMessageConstants.EmailAdressInvalid)]

What am i missing here? This regular expression is off the following post: DataAnnotations validation (Regular Expression) in asp.net mvc 4 - razor view

Community
  • 1
  • 1
learning...
  • 2,759
  • 6
  • 51
  • 84

2 Answers2

7

Out of the box, .NET 4.5 has the System.ComponentModel.DataAnnotations.EmailAddressAttribute found in the System.ComponentModel.DataAnnotations assembly, but this is limited to validating just one email address. Hence, if you have a model that accepts delimitted email address and you decorate the property with this attribute, it will fail since it will treat the entire string as one email.

What I've done is create an extended emailaddress attribute that validates the delimited email addresses:

    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class EmailAddressExAttribute : DataTypeAttribute
{
    #region privates
    private readonly EmailAddressAttribute _emailAddressAttribute = new EmailAddressAttribute();
    #endregion

    #region ctor
    public EmailAddressExAttribute() : base(DataType.EmailAddress){ }
    #endregion

    #region Overrides
    /// <summary>
    /// Checks if the value is valid
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public override bool IsValid(object value)
    {
        var emailAddr = Convert.ToString(value);
        if (string.IsNullOrWhiteSpace(emailAddr)) return false;

        //lets test for mulitple email addresses
        var emails = emailAddr.Split(new[] {';', ' ', ','}, StringSplitOptions.RemoveEmptyEntries);
        return emails.All(t => _emailAddressAttribute.IsValid(t));
    }
    #endregion

}

You can now decorate any string property with this new extended attribute to validate delimited email addresses. You can update the delimiters to include any special characters you want to use as well.

Hope this helps!

UT-Fan-05
  • 346
  • 3
  • 7
3

You not stating what the question is, so I will have to assume from your answer that data annotations aren't working as you would expect.

Having that assumption in mind, its very easy why is it not working: data annotation operates on the entire field, text area in your case. It will work as expected if you have only one email. Since you have multiple emails in that field, separated by comma or space, the field in its entirety doesn't reflect what data annotation for email prescribes and fails.

To answer your numbered questions:

  1. No, there is no out of the box

  2. The regular expression you using doesn't account for multiple emails, but one. The solution in your case will be either to

Following the links above you will see very good examples of "how to" and hopefully get you going in the right direction. Hope this helps, please let me know if not.

Community
  • 1
  • 1
Display Name
  • 4,676
  • 1
  • 29
  • 43