12

How can I set the string validation for a fixed-length string using data annotation ?

I want to have a property on my model with the exact 10 string-length.

I'm counting the seconds for help.

Varan Sinayee
  • 1,105
  • 2
  • 12
  • 26

2 Answers2

27

1) [StringLength(10, MinimumLength = 10)]

2) [RegularExpression(@"(.{10})")]

Max
  • 1,784
  • 2
  • 19
  • 26
14

Try

[StringLength(10, MinimumLength = 10, ErrorMessage = "This field must be 10 characters")]
public string myString {get;set;}
aspirant_sensei
  • 1,568
  • 1
  • 16
  • 36