Organizational Research By

Surprising Reserch Topic

mvc 4 dataannotations validation for type using -',,data-annotations'

mvc 4 dataannotations validation for type  using -',,data-annotations'

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; }


asked Sep 7, 2015 by rajesh
0 votes

Related Hot Questions

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.