Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation for numeric but not equal to 0 in MVC4

I want to use DataAnnotations in mvc4 for valid value only for numeric but greater than 0, Please help.

Thanks.

like image 624
Hemant Soni Avatar asked Apr 18 '13 06:04

Hemant Soni


People also ask

What are the types of validations in MVC?

There are two types of validations: Server side Validations. Client Side Validations.

Can we do validation in MVC using data annotations?

In ASP.NET MVC, Data Annotation is used for data validation for developing web-based applications. We can quickly apply validation with the help of data annotation attribute classes over model classes.

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.


1 Answers

You could use the [Range] attribute:

[Range(1, int.MaxValue, ErrorMessage = "The value must be greater than 0")]
public int Value { get; set; }
like image 161
Darin Dimitrov Avatar answered Oct 01 '22 12:10

Darin Dimitrov