madvora
madvora

Reputation: 1747

MVC5 DataAnnotations - Accept Specific Numbers

How can I set a ViewModel to accept only specific values with a data annotation? For example, I only want the input values from a form to be 2 or 4. I'm looking for something like [Range(2, 4)], but that still accepts numbers in between. I want to list the specific numbers to accept.

[Required]
public int NumberParticipants { get; set; }

Also looking for the same thing with strings.

Upvotes: 0

Views: 86

Answers (1)

BhavO
BhavO

Reputation: 2399

[RegularExpression("^(2|4)$", ErrorMessage = "Please enter 2 or 4")]

Upvotes: 1

Related Questions