asp.net-mvc mvc [DataType(DataType.EmailAddress) 没有验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11453574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:06:17  来源:igfitidea点击:

mvc [DataType(DataType.EmailAddress) no validation

asp.net-mvcasp.net-mvc-3data-annotations

提问by GibboK

I'm using this code on an email field:

我在电子邮件字段上使用此代码:

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

[DataType(DataType.EmailAddress)]does not work (validation does not occur no at a server not on the client side).

[DataType(DataType.EmailAddress)]不起作用(验证不会发生在不在客户端的服务器上)。

I am not sure if I should implement myself a Custom Attribute or I can use one included with MVC 3.

我不确定是否应该自己实现自定义属性,或者我可以使用 MVC 3 中包含的自定义属性。

Could you please suggest me a solution for creating a custom attribute in case I need to.

如果我需要,您能否建议我创建自定义属性的解决方案。

I read also about some additional extensions, example http://nuget.org/packages/DataAnnotationsExtensions.MVC3

我还阅读了一些其他扩展,例如 http://nuget.org/packages/DataAnnotationsExtensions.MVC3

Would you suggest it to me?

你会推荐给我吗?

采纳答案by GibboK

At the moment I have solved my problem using DataAnnotationsExtensions

目前我已经使用 DataAnnotationsExtensions 解决了我的问题

it just works, you add their library with NuGet

它只是有效,您可以使用 NuGet 添加他们的库

using DataAnnotationsExtensions;


[Required]
    [DataType(DataType.EmailAddress)]
    [Email]
    public string Email { get; set; }

回答by SarahK

You could use the usual DataAnnotations library by just using [EmailAddress]

您只需使用 [EmailAddress] 即可使用通常的 DataAnnotations 库

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

Also just for reference, here's the regular expression version of this validation:

也仅供参考,这是此验证的正则表达式版本:

    [RegularExpression(@"^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-??]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$", ErrorMessage = "Email is not valid")]
    public String Email {get; set;}

Best of luck!

祝你好运!

回答by Felix

It looks like all the answers focus on the Data Model while this issue can be affected by the View itself.

看起来所有的答案都集中在数据模型上,而这个问题可能会受到视图本身的影响。

The following on MVC .NET 4.5 is working alright:

以下 MVC .NET 4.5 工作正常:

Data model:

数据模型:

[Required]
[DataType(DataType.EmailAddress)]
[DisplayName("Email")]
public string Email { get; set; }

Razor View:

剃刀视图:

@Html.LabelFor(model => model.Email)
@Html.EditorFor(model => model.Email)

Note:do not need to add [EmailAddress] attribute. If you use [DataType(DataType.EmailAddress)] along with @Html.EditorFor() in your View, you should be fine.

注意:不需要添加 [EmailAddress] 属性。如果您在视图中使用 [DataType(DataType.EmailAddress)] 和 @Html.EditorFor() ,则应该没问题。

As highlighted with rich.okelly, at the end you want your input rendered as <input type="email" />.

正如 Rich.okelly 所强调的那样,最后你希望你的输入呈现为<input type="email" />.

回答by msib

May be this will be helpful for someone. Following works for me

可能这对某人有帮助。以下对我有用

[Required(ErrorMessage = "*")]
[DataType(DataType.EmailAddress)]
[EmailAddress]
public string Email { get; set; }

But does not work following

但以下不起作用

[Required(ErrorMessage = "*")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }

I am using MVC 5& .NET 4.5

我正在使用MVC 5&.NET 4.5

回答by Stacked

As Felix mentioned, the problem is on the Viewlevel, you need to use EditorFor()in your Viewinstead of TextBoxFor(), the EditorFor()will render:

正如Felix 所提到的,问题出在View级别上,您需要使用EditorFor()in yourView而不是TextBoxFor()EditorFor()将呈现:

<input type="email" />

which will trigger the validation, while TextBoxFor()will render:

这将触发验证,而TextBoxFor()将呈现:

<input type="text" />

So in order to validate your entered email address, you need (in combination with EditorFor()) to use only:

因此,为了验证您输入的电子邮件地址,您需要(与 结合使用EditorFor())仅使用:

[DataType(DataType.EmailAddress)]
public string Email { get; set; }

This way, your entered value for email will be always validated, but if you don't enter a value for email, nothing will happen (unless you specified the [Required]attribute), the form will be submitted with an empty email address.

这样,您输入的电子邮件值将始终被验证,但如果您不输入电子邮件值,则不会发生任何事情(除非您指定该[Required]属性),表单将使用空电子邮件地址提交。