Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating an email string in .net using EmailAddressAttribute, but not on an attribute

I want to be able to do this:

string email = "[email protected]";
bool isValid = IsValidEmail(email);
//returns true

...but use the logic Microsoft has already given us in EmailAddressAttribute.

There are hundreds of good and bad implementations of IsValidEmail out there, and I don't want to add my own, when I can use something, Microsoft has already thought about. It stands to reason, that they will do a better job than me.

In recent ASP.Net versions, we have System.ComponentModel.DataAnnotations, which provides us with the excellent EmailAddressAttribute, that lets us decorate our models like this:

public class SomeModel
{
    [EmailAddress]
    public string Email { get; set; }
}

The class is sealed, so I cannot call EmailAddressAttribute.IsValid()

Is there a good way I can utilize what Microsoft has already done? Is there a method somewhere in the .Net framework, that let's me test strings against data-annotations, that I have overlooked?

Something along the lines of..:

var emailChecker = new DataAnnotationsChecker<EmailAddressAttribute>();
string email = "[email protected]";
bool isValid = emailChecker.IsValid(email);
//returns true

If you cannot imagine why I would want this, think about getting a List and want to check which ones are valid emails.

like image 849
Kjensen Avatar asked Apr 29 '16 21:04

Kjensen


People also ask

What is System ComponentModel DataAnnotations?

Data annotations (available as part of the System. ComponentModel. DataAnnotations namespace) are attributes that can be applied to classes or class members to specify the relationship between classes, describe how the data is to be displayed in the UI, and specify validation rules.

What is data annotation validator attributes in MVC?

Data annotation attributes are attached to the properties of the model class and enforce some validation criteria. They are capable of performing validation on the server side as well as on the client side. This article discusses the basics of using these attributes in an ASP.NET MVC application.


1 Answers

You could use the EmailAddressAttribute to do the validation.

The sealed means that you cannot create another class that inherits from it. Doesn't mean you cannot use it.

Created some unit tests and works fine

[TestMethod]
public void Should_Use_Email_Address_Attribute_To_Validate_Email() {
    var emailChecker = new System.ComponentModel.DataAnnotations.EmailAddressAttribute();
    string email = "[email protected]";
    bool isValid = emailChecker.IsValid(email);
    Assert.IsTrue(isValid);
}

[TestMethod]
public void Should_Use_Email_Address_Attribute_To_Invalidate_Email() {
    var emailChecker = new System.ComponentModel.DataAnnotations.EmailAddressAttribute();
    string email = "some@emai l.com";
    bool isValid = emailChecker.IsValid(email);
    Assert.IsFalse(isValid);
}
like image 155
Nkosi Avatar answered Oct 13 '22 11:10

Nkosi