If you are a developer, you understand how important it is to validate user
input. One of the most common types of validation is email validation. In this
article, we will take a deep dive into email validation data annotation in
ASP.NET MVC.

What is Email Validation Data Annotation?

email-validation
Email validation data annotation is a built-in validation attribute in ASP.NET
MVC. It is used to validate email addresses entered by users in a form. This
data annotation checks whether the email address entered by the user is valid
or not. If the email address is not valid, an error message is displayed to
the user.

How to Use Email Validation Data Annotation?

Using email validation data annotation is very simple. You just need to add
the [EmailAddress] attribute to your model property that represents the email
address. Here is an example:

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

In the above example, the Email property is decorated with the [EmailAddress]
attribute. This will ensure that only valid email addresses are accepted as
input.

Client-Side Validation

email-validation
Client-side validation is a type of validation that occurs on the client-side
(i.e., in the user's browser) before the form is submitted to the server. This
type of validation is useful because it provides immediate feedback to the
user and saves server resources. Email validation data annotation supports
client-side validation out of the box. All you need to do is include the
necessary JavaScript files in your view. Here is an example:

<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

Once you have included these files, client-side validation will be
automatically enabled for your email validation data annotation.

Server-Side Validation

Server-side validation is a type of validation that occurs on the server-side
(i.e., on the server) after the form is submitted. This type of validation is
important because it provides an extra layer of security and ensures that only
valid data is processed by your application. Email validation data annotation
also supports server-side validation. Here is an example:

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

 [Required]
 public string Password { get; set; }

 public bool RememberMe { get; set; }

 public async Task<ClaimsIdentity> GetClaimsIdentityAsync(UserManager<ApplicationUser> userManager)
 {
 // ... code to get claims identity ...
 }
}

In the above example, the Email property is decorated with both the [Required]
and [EmailAddress] attributes. This will ensure that the email address entered
by the user is not only valid but also required.

Conclusion

Email validation data annotation is a simple yet powerful way to validate user
input in ASP.NET MVC. It provides both client-side and server-side validation
and ensures that only valid email addresses are accepted as input. By using
email validation data annotation in your application, you can improve the user
experience and the security of your application.