简体   繁体   English

asp / .net / mvc中Datatype.EmailAddress的使用

[英]Uses of Datatype.EmailAddress in asp/.net/mvc

I have a Account Model in which I am using Email Address as Username 我有一个帐户模型,我在其中使用电子邮件地址作为用户名

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

I have designed a custom class to verify email. 我设计了一个自定义类来验证电子邮件。 But I recently noticed that the DataType.EmailAddress . 但我最近注意到了DataType.EmailAddress I tried to use this Datatype as shown in code above to check if I can validate the Username without my Custom Class but it fails. 我尝试使用上面代码中显示的此数据类型来检查我是否可以在没有我的自定义类的情况下验证用户名但是它失败了。 So my question is how is this DataType useful in .NET . 所以我的问题是这个DataType在.NET是如何有用的。 It seems to be doing nothing on my Registration Form. 它似乎在我的注册表上无所作为。

Edit: It dosent even validate against a regex. 编辑:甚至对正则表达式进行验证。 For example Username: SS, ssssss, tttt, etc all pass off as valid emails. 例如,用户名: SS, ssssss, tttt, etc作为有效电子邮件传递。

Edit: People I have a class to validate email in the code behind. 编辑:人我有一个类来验证后面的代码中的电子邮件。 I know hat are the complexities of validating Emails. 我知道帽子是验证电子邮件的复杂性。 I am not asking how to validate email. 我不是问如何验证电子邮件。 I am just asking about the uses of this datatype. 我只是问这个数据类型的用法。

So, you are asking what this data type does not why isn't it validating, correct? 所以,你问这个数据类型不是为什么不验证,对吗? Per the MSDN, DataType attributes are used primarily for formatting and not validation (which you have learned). 根据MSDN,DataType属性主要用于格式化而不是验证(您已经学习过)。 What this attribute should do, is when using the Html.DisplayFor() helper, render the field as a clickable hyperlink. 此属性应该执行的操作是,在使用Html.DisplayFor()帮助程序时,将该字段呈现为可单击的超链接。

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

Renders 呈现

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

Additionally, as pointed out by Zhaph in the comments below, using it in the Html.EditorFor() will generate an HTML 5 email input, which looks something like this: 另外,正如Zhaph在下面的评论中所指出的,在Html.EditorFor()它将生成HTML 5电子邮件输入,如下所示:

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

From MSDN 来自MSDN

The following example uses the DataTypeAttribute to customize the display of EmailAddress data field of the customer table in the AdventureWorksLT database. 以下示例使用DataTypeAttribute自定义AdventureWorksLT数据库中customer表的EmailAddress数据字段的显示。 The e-mail addresses are shown as hyperlinks instead of the simple text that ASP.NET Dynamic Data would have inferred from the intrinsic data type. 电子邮件地址显示为超链接,而不是ASP.NET动态数据从内部数据类型推断出的简单文本。

DataType alone will not trigger any server-side validation. DataType不会触发任何服务器端验证。 But, since MVC 4 using DataType.EmailAddress will make the HTML input use type="email" , which in turn makes jQuery Validation perform Regex validation on the client. 但是,由于使用DataType.EmailAddress MVC 4将使HTML输入使用type="email" ,这反过来使jQuery Validation在客户端上执行Regex验证。

.NET 4.5 introduced the [EmailAddress] attribute, a subclass of DataTypeAttribute . .NET 4.5引入了[EmailAddress]属性,它是DataTypeAttribute的子类。 By using [EmailAddress] you get both client and server side validation. 通过使用[EmailAddress]您可以获得客户端和服务器端验证。

you can use the EmailAddress data annotation or the regex to solve this issue. 您可以使用EmailAddress数据注释或正则表达式来解决此问题。 Date type is used to tell the html helper to render the html for the view. 日期类型用于告诉html帮助器呈现视图的html。

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

Datatype.Emailaddress derives from DataTypeAttribute and adds client-side e-mail validation you also need to set <% Html.EnableClientValidation(); %> Datatype.Emailaddress派生自DataTypeAttribute并添加客户端电子邮件验证,您还需要设置<% Html.EnableClientValidation(); %> <% Html.EnableClientValidation(); %> in your corresponding view. <% Html.EnableClientValidation(); %>在相应的视图中。

Alternatively you could use the DataAnnotations library by using EmailAddress (This performs server side validation) 或者,您可以使用EmailAddress (这执行服务器端验证)使用DataAnnotations库

using System.ComponentModel.DataAnnotations; 

    [Required]
    [EmailAddress]
    public String Email { get; set; }

This is the regex to validate Email address 这是验证电子邮件地址的正则表达式

[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 custom email validation Attribute. 您还可以创建自定义电子邮件验证属性。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM