0

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

user2687153
  • 427
  • 5
  • 24
  • 1
    _abcde_ cannot be parsed to a decimal, so it displays that error. If you were to enter `0` or `1001` you should get the error associated with the range attribute. –  Apr 13 '15 at 12:19
  • Probably, but I'd like the custom error message be displayed each time an incorect input is set, "abcde", 1500, -400 or anything which is not is the given range. How could I achieve this ? – user2687153 Apr 13 '15 at 12:48
  • Check out my answer below to achieve this – Mansoor Apr 13 '15 at 12:49
  • [This answer](http://stackoverflow.com/questions/6214066/how-to-change-default-validation-error-message-in-asp-net-mvc) might help. Also some extra information in [this blog](http://www.dotnet-programming.com/post/2012/01/17/Customizing-Error-Messages-in-AspNet-MVC.aspx) –  Apr 13 '15 at 12:52

1 Answers1

1

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.

Mansoor
  • 743
  • 11
  • 18
  • This seems to works, thanks a lot ! I'm a little bit disappointed I have to add the RegularExpression tag, I hoped ``Range` could do the trick itself. But I understand the explanation, so, well... Thanks a lot :-) – user2687153 Apr 13 '15 at 12:55