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; }
You can try with the regular expression:
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)
Or using Range with a slight modification to @Martin suggestion (actually is a better solution):