使用ASP.NET MVC数据类型属性的电子邮件地址验证 [英] Email address validation using ASP.NET MVC data type attributes

查看:1096
本文介绍了使用ASP.NET MVC数据类型属性的电子邮件地址验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题与电子邮件的验证。

I have some problems with the validation of a Email.

在我的模型:

[Required(ErrorMessage = "Field can't be empty")]
[DataType(DataType.EmailAddress, ErrorMessage = "E-mail is not valid")]
public string ReceiverMail { get; set; }

在我看来:

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@Html.TextBoxFor(m => m.ReceiverMail, new { @placeholder="E-mail"}) <br />
@Html.ValidationMessageFor(m => m.ReceiverMail)

现在它被正确地显示我字段不能为空当你离开的字段为空。
但是,当你在这样一个无效的电子邮件地址填写:fwenrjfw,那么形式不说电子邮件是无效的

Now it is correctly showing me "Field can't be empty" when you leave the field empty. But when you fill in an invalid email address like: "fwenrjfw" then the form does not say "E-mail is not valid".

我怎样才能得到验证输入作为电子邮件地址的形式?我在寻找一些帮助与此有关。

How can I get the form to validate the input as an email address? I am looking for some help with this.

推荐答案

如果您使用的是.NET框架4.5的解决方案是使用 EmailAddressAttribute 驻留在 System.ComponentModel.DataAnnotations

If you are using .NET Framework 4.5, the solution is to use EmailAddressAttribute which resides inside System.ComponentModel.DataAnnotations.

您code应类似于此:

Your code should look similar to this:

[Display(Name = "Email address")]
[Required(ErrorMessage = "The email address is required")]
[EmailAddress(ErrorMessage = "Invalid Email Address")]
public string Email { get; set; }

这篇关于使用ASP.NET MVC数据类型属性的电子邮件地址验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆