-1

I've built a model in asp.net (visual studio 2017) and I want to put some validations in my code.

When I'm trying to update the database I've got this messasge:

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details

When I'm deleting the regular expression there is no problem.

This is my code:

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; }
}

This is the right syntax? cause I've checked them many times and its supposed to work.

This is one of the items I want to enter the DB:

new Customer {customerID=311111111, firstName= "Lior", lastName="David", phone="0549121111", carNumber=57382561, mailCustomer="Liorda@gmail.com", homeAddress="bograshov 22", workAddress="bograshov 18"}, 
J. Steen
  • 15,470
  • 15
  • 56
  • 63
Tamir Hazan
  • 63
  • 1
  • 9

1 Answers1

0

I'm not 100% but it seems likely to be a problem with your CarNumber property. RegularExpression attributes will work fine on string properties but using a Range attribute may be better on your int property:

[Range(0, int.MaxValue, ErrorMessage = "Please enter valid integer Number")]
Mark Rawson
  • 116
  • 4