I'm developping an ASP.NET MVC3 application.
I use DataAnnotations
for some properties of my Models, with a custom error message if input is not correct.
However, it perfectly works when I run my app in localhost, but when I put it on my webserver (which is a shared web server), the error message is the one by default.
Here is an example :
[Required]
[Range(1d, 1000d, ErrorMessage = "My custom error message in French")]
public decimal Surface { get; set; }
When user type "abcde" in Surface
field, I have the following error message :
The field Surface must be a number.
However, it should display the specified ErrorMessage. This works in localhost but not on my webserver.
I tried to force culture in web.config
as follow :
<globalization culture="fr-FR" uiCulture="fr-FR"/>
But this doesn't work.
How can I force the application to display the ErrorMessage
?
I can't modify anything on the web server which hosts my app, it's a personal project hosted on a local website hosting.
Thanks for your help
As Stephen Muecke wrote "abcde" cannot be parsed into a decimal so it shows the generic error that "The field Surface must be a number".
To make the error message as you custom message you need to check if the input is a number/decimal and you can do it with a regular expression. Change your code as follows
[Required]
[RegularExpression(@"^\d+.\d{0,2}$",ErrorMessage = "custom error in your language")]// add this extra line
[Range(1d, 1000d, ErrorMessage = "My custom error message in French")]
public decimal Surface { get; set; }
You need to add the following line
[RegularExpression(@"^\d+.\d{0,2}$",ErrorMessage = "custom error in your language")]
which check via regular expression if it is a number, it will also check the decimal points if they are more than 2 like 2.002 then it will also give the error. You can look into regular expressions to build another expression which suits you the best.