正则表达式 - asp.net,c# - 验证失败

时间:2017-12-23 09:30:18

标签: c# asp.net asp.net-mvc

我在asp.net(visual studio 2017)中构建了一个模型,我想在我的代码中加入一些验证。

当我试图更新数据库时,我得到了这个消息:

  

一个或多个实体的验证失败。请参阅' EntityValidationErrors'物业详情

当我删除正则表达式时,没有问题。

这是我的代码:

year-month-day hour:minutes:seconds,milliseconds

这是正确的语法吗?因为我已多次检查它们并且它应该有效。

这是我想要输入数据库的项目之一:

public class Customer
{
    public int customerID { set; get; }

    [Display(Name = "First Name")]
    [RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Must be alphabates only")]
    [StringLength(20, MinimumLength = 2)]
    public string firstName { set; get; }

    [Display(Name = "Last Name")]
    [RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Must be alphabates only")]
    [StringLength(20, MinimumLength = 2)]
    public string lastName { set; get; }

    [Display(Name = "Phone")]
    [RegularExpression(@"^[0-9]+$", ErrorMessage = "Must be  numbers only")]
    [StringLength(10, MinimumLength = 9)]
    public string phone { set; get; }

    [Display(Name = "Car Number")]
    [RegularExpression(@"^[0-9]+$", ErrorMessage = "Must be  numbers only")]

    public int carNumber { set; get; }


    [Display(Name = "e-mail Customer")]
    [RegularExpression(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Must be valid email")]
    [StringLength(30, MinimumLength = 4)]
    public string mailCustomer { set; get; }


    [Display(Name = "Home Address")]
    [RegularExpression(@"^[a-zA-Z0-9''-'\s]*$", ErrorMessage = "Must be alphabates and numbers only")]
    [StringLength(40, MinimumLength = 2)]
    public string homeAddress { set; get; }


    [Display(Name = "Work Address")]
    [RegularExpression(@"^[a-zA-Z0-9''-'\s]*$", ErrorMessage = "Must be alphabates and numbers only")]
    [StringLength(40, MinimumLength = 2)]
    public string workAddress { set; get; }
}

1 个答案:

答案 0 :(得分:0)

我不是100%但是你的CarNumber属性似乎有问题。 RegularExpression属性在字符串属性上可以正常工作,但在int属性上使用Range属性可能更好:

[Range(0, int.MaxValue, ErrorMessage = "Please enter valid integer Number")]