Using Datatype.EmailAddress in asp / .net / mvc

I have an account model in which I use the email address as the username

public class RegisterModel { [Required] [Display(Name = "Email Address")] [DataType(DataType.EmailAddress)] public string UserName { get; set; } 

I developed a special class for checking email. But I recently noticed that DataType.EmailAddress . I tried using this data type as shown in the above code to check if I can check the username without my user class, but it fails. So my question is how is this DataType useful in .NET . It seems that I am not doing anything in my Registration form.

Edit: An assistant professor even checks for regular expression matching. For example, Username: SS, ssssss, tttt, etc all sent as valid emails.

Edit: People I have a class for checking email in code behind. I know that a hat is the difficulty of checking emails. I do not ask how to check email. I am just asking about using this data type.

+6
source share
4 answers

So, you ask what this data type does not, why does it not check, does not fix? In MSDN, the DataType attributes are used primarily for formatting, not for validation (which you recognize). This attribute should do when the Html.DisplayFor() helper is used, render this field as an interactive hyperlink.

 @Html.DisplayFor(x=>x.UserName) 

Visualizes

 <a href="mailto:{0}">{0}</a> 

Also, as Zhaph noted in the comments below, using it in Html.EditorFor() will generate an HTML 5 HTML email address that looks something like this:

 <input type="email".../> 

From MSDN

The following example uses the DataTypeAttribute attribute to configure the mapping of the EmailAddress data field of the client table to the AdventureWorksLT database. The email address is displayed as hyperlinks instead of the plain text that the dynamic ASP.NET data was inferred from the native data type.

+13
source

DataType will not initiate server-side validation on DataType own. But since MVC 4 using DataType.EmailAddress will make use of type="email" HTML code, which in turn will force jQuery Validation to perform a Regex check on the client.

.NET 4.5 introduced the [EmailAddress] attribute, a subclass of DataTypeAttribute . Using [EmailAddress] , you receive confirmation on the client and server side.

+12
source

You can use EmailAddress data annotation or regular expression to solve this problem. The date type is used to indicate to the html helper to render the html for presentation.

 [EmailAddress] [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Must be a valid Email Address")] 
+4
source

Datatype.Emailaddress comes from DataTypeAttribute and adds client-side email verification, you also need to set <% Html.EnableClientValidation(); %> <% Html.EnableClientValidation(); %> to the appropriate view.

Alternatively, you can use the DataAnnotations library using EmailAddress (This does a server side check)

 using System.ComponentModel.DataAnnotations; [Required] [EmailAddress] public String Email { get; set; } 

This is a regular expression for validating email addresses.

 [Required(ErrorMessage="Email is required")] [RegularExpression(@"[A-Za-z0-9._%+-]+[A-Za-z0-9.-]+\.[A-Za-z] {2,4}", public String Email {get; set;} 

You can also create your own email authentication attribute.

http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

+2
source

Source: https://habr.com/ru/post/956162/


All Articles