Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 4 - DataAnnotations - Validation for Type

I have the following code working

    [Required(ErrorMessage = "Price is required.")]
    [Range(typeof(Decimal), "1", "9999", ErrorMessage = "Price xx.xx")]
    public decimal? productPrice { get; set; }

When the page is submitted with Price = EMPTY Field error message is "Price is required.". Price = over 9999 error message is "Price xx.xx".

However, when I type 'aaaa' the error message is "The field productPrice must be a number."

How can I change the message if type in not correct? Like : "Price must be a decimal/number between 1-9999.

---- UPDATE: ---- The below code worked with

NULL, Not Decimal, Between Range, BUT not working with ".1".

    [Required(ErrorMessage = "Price is required.")]
    [RegularExpression(@"[0-9]*\.?[0-9]+", ErrorMessage = "Price must be a Numbers only.")]
    [Range(typeof(Decimal), "1", "9999", ErrorMessage = "Price must be a decimal/number between {1} and {2}.")]
    public decimal? productPrice { get; set; }
like image 654
Ravi Ram Avatar asked Aug 28 '12 17:08

Ravi Ram


People also ask

What are the types of validation in MVC?

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

How do you validate model data using DataAnnotations attributes?

ComponentModel. DataAnnotations namespace includes the following validator attributes: Range – Enables you to validate whether the value of a property falls between a specified range of values. RegularExpression – Enables you to validate whether the value of a property matches a specified regular expression pattern.

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.

How can we make a field mandatory in Cshtml?

cshtml. Add the "[Required]" attribute to the field that you want to make mandatory for insertion. The required attribute requires the "System. ComponentModel.


2 Answers

You can try with the regular expression:

[RegularExpression(@"[0-9]*\.?[0-9]+", ErrorMessage = "{0} must be a Number.")]

you can also try the Data Annotations Extensions: http://dataannotationsextensions.org/Home/Wiki

Or write your own implementation,something like this : https://github.com/srkirkland/DataAnnotationsExtensions/blob/master/DataAnnotationsExtensions/DigitsAttribute.cs

UPDATE With REGEX (Matches $9,999.99 | $0.70 | .1)

[RegularExpression(@"^\$?([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}[0-9]{0,}(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$", ErrorMessage = "{0} must be a Number.")]

Or using Range with a slight modification to @Martin suggestion (actually is a better solution):

[Range(typeof(Decimal), "0", "9999", ErrorMessage = "{0} must be a decimal/number between {1} and {2}.")]
like image 127
JOBG Avatar answered Oct 17 '22 12:10

JOBG


First off, I think you will want to change your Range attribute to

[Range(typeof(Decimal), "1", "9999", ErrorMessage = "{0} must be a decimal/number between {1} and {2}.")]

According to MSDN, this is the valid way to use RangeAttribute.

Second:

"The field productPrice must be a number."

This is actually unobtrusive client-side JavaScript validation kicking in. Your range validator will fire after the number has been validated. You can disable the number validator although I do not recommend this:

$.validator.methods.number = function (n, t) {
    return true;
}
like image 38
Martin Devillers Avatar answered Oct 17 '22 14:10

Martin Devillers