使用ASP.NET MVC数据类型属性进行电子邮件地址验证
使用ASP.NET MVC数据类型属性进行电子邮件地址验证
我在验证邮箱方面遇到了一些问题。\n在我的模型中:\n
[Required(ErrorMessage = "字段不能为空")] [DataType(DataType.EmailAddress, ErrorMessage = "邮箱格式不正确")] public string ReceiverMail { get; set; }
\n在我的视图中:\n\n\n@Html.TextBoxFor(m => m.ReceiverMail, new { @placeholder=\"邮箱\"})
\n现在当你留空该字段时,它会正确地显示\"字段不能为空\"。\n但是当你填写一个无效的邮箱地址,如:\"fwenrjfw\",表单不会显示\"邮箱格式不正确\"。\n我该如何让表单验证输入为有效的邮箱地址?我需要一些帮助。
\n@Html.ValidationMessageFor(m => m.ReceiverMail)\n
Email地址验证是使用ASP.NET MVC数据类型属性的一个问题。原因是需要验证输入的Email地址是否有效,以防止用户输入无效的Email地址。解决方法是使用RegularExpression属性来验证Email地址的格式。同时,也要确保不删除Required属性,因为RegularExpression属性不会影响空字段。使用RegularExpression属性可以避免一些错误和疏忽,并且适用于各种情况、国家、时区和星球。然而,该正则表达式可能无法处理带有特殊字符或非标准字符的Email地址。因此,在使用正则表达式验证Email地址之前,最好参考一些可靠的资源。另外,Microsoft的EmailAddress数据注释接受名称作为有效的Email地址,但实际上这可能是错误的。为了更可靠地验证Email地址,不推荐使用正则表达式,可以参考一些其他的方法和资源。
Email address validation using ASP.NET MVC data type attributes is a common problem faced by developers when working with email input fields in ASP.NET MVC. The issue arises because the default data type validation provided by ASP.NET MVC does not have built-in support for email address validation. This means that if a user enters an invalid email address, the validation will not catch it and the form will be submitted with the invalid email.
One solution to this problem is to use the Html.EditorFor helper method instead of Html.TextBoxFor. By using Html.EditorFor, the data type and error already added in the model will be used for validation. This means that if the model has a data annotation for email address validation, it will be applied when rendering the input field.
However, it is important to note that even with Html.EditorFor, the built-in validation does not check for the domain top-level domain (TLD) of the email address. This means that someone could enter a valid email address without including the TLD, and it would still pass the validation. For example, "myname" would be considered a valid email address.
Despite this limitation, the use of Html.EditorFor with the model's data annotation for email address validation is still an effective and convenient solution for email address validation in ASP.NET MVC. It provides a basic level of validation and can be easily implemented in MVC 4.
If you are looking for more advanced email address validation, such as checking for the presence of a TLD, you may need to consider using additional validation techniques or third-party libraries. However, for most basic email address validation needs, Html.EditorFor with the model's data annotation should suffice.
To implement email address validation using Html.EditorFor, simply add the appropriate data annotation to the model property representing the email address. For example:
public class UserModel { [DataType(DataType.EmailAddress)] public string Email { get; set; } }
Then, in your view, use Html.EditorFor to render the email input field:
@Html.EditorFor(model => model.Email)
By following these steps, you can easily implement email address validation using ASP.NET MVC data type attributes and Html.EditorFor method.
Email地址验证使用ASP.NET MVC数据类型属性的问题是因为使用了错误的属性来验证邮箱地址。解决方法是使用EmailAddressAttribute属性来验证邮箱地址,而不是使用DataType(DataType.EmailAddress)属性。以下是解决方法的示例代码:
如果你使用的是.NET Framework 4.5,解决方法是使用System.ComponentModel.DataAnnotations命名空间中的EmailAddressAttribute属性。
你的代码应该类似于这样:
[Display(Name = "Email address")] [Required(ErrorMessage = "The email address is required")] [EmailAddress(ErrorMessage = "Invalid Email Address")] public string Email { get; set; }
感谢Olugbenga的回答!但我不明白为什么这个方法不起作用:
[DataType(DataType.EmailAddress, ErrorMessage = "Error message.")]
Zanelli - DataType(DataType.EmailAddress)不能用来验证用户输入。它只是用来为显示/编辑模板提供一个UI提示。
DataType.EmailAddress与验证无关,它只与数据展示相关。
我遇到了同样的问题,对我来说,除了类似于'name'这样的邮箱之外,其他邮箱都能正常验证。还有其他人遇到过类似的问题吗?
'name'是一个完全有效的邮箱地址,所以这个方法没有问题。举个例子,可以考虑使用'username'。
实际上,在进一步调查后,发现失败的是Required注解,而不是EmailAddress注解。
为什么你认为"这个回答是错误的,应该忽略"?'EmailAddress'属性由微软提供,并且已经存在多年。
对我来说没有起作用,直到我在视图中包含了jQuery的引用。
有没有办法强制要求邮箱地址中包含.com、.org或者其他格式?
'name'不是一个有效的邮箱地址。试着用这个邮箱地址购买任何Adobe产品,你就会知道了。