我需要你的帮助,我已经写了这个类来保存数据,在我的例子中我用 DataAnnotation 进行验证,不幸的是我输入了无效的电子邮件地址,但它没有反对所以我我对使用 DataAnnotations
的正确方法感到困惑using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace workflow.DataHolders
{
public class NewCompany
{
[Required]
[StringLength(200, MinimumLength = 3, ErrorMessage="Length Should Be More Than Three Letters")]
public string CompanyName { get; set; }
[Required]
[EmailAddress(ErrorMessage="Invalid Email Address")]
public string Email { get; set; }
[Required]
[StringLength(200, MinimumLength = 2, ErrorMessage = "Length Should Be More Than Two Letters")]
public string Country { get; set; }
public string Description { get; set; }
}
}
答案 0 :(得分:0)
我们有两种方法可以做到这一点
第一路
[RegularExpression(@"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", ErrorMessage = "Invalid Email Address")]
public string email { get; set; }
第二路
[DataType(DataType.EmailAddress)]
[Email]
public string email { get; set; }