Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Mvc Fixed-Length String Data Annotation

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.

like image 791
Varan Sinayee Avatar asked Apr 21 '16 12:04

Varan Sinayee


People also ask

What is the data annotation in MVC?

DataAnnotations is used to configure your model classes, which will highlight the most commonly needed configurations. DataAnnotations are also understood by a number of . NET applications, such as ASP.NET MVC, which allows these applications to leverage the same annotations for client-side validations.

Can we do validation in MVC using data annotations?

In Asp.net MVC, we can easily apply validation to web application by using Data Annotation attribute classes to model class. Data Annotation attribute classes are present in System.

What is System ComponentModel DataAnnotations?

Data annotations (available as part of the System. ComponentModel. DataAnnotations namespace) are attributes that can be applied to classes or class members to specify the relationship between classes, describe how the data is to be displayed in the UI, and specify validation rules.

Which data annotation value specifies the max and min length of string with error?

The StringLength Attribute Specifies both the Min and Max length of characters that we can use in a field.


2 Answers

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

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

like image 191
Max Avatar answered Oct 19 '22 17:10

Max


Try

[StringLength(10, MinimumLength = 10, ErrorMessage = "This field must be 10 characters")]
public string myString {get;set;}
like image 42
aspirant_sensei Avatar answered Oct 19 '22 19:10

aspirant_sensei