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; }
There are two types of validations: Server side Validations. Client Side Validations.
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.
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.
cshtml. Add the "[Required]" attribute to the field that you want to make mandatory for insertion. The required attribute requires the "System. ComponentModel.
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}.")]
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With