6

Product.cs

...
    [Required(ErrorMessage="Price is required")]
    [Range(0.01, 100000.00,
        ErrorMessage="Price must be between 0.01 and 100000.00")]
    public decimal Price { get; set; }
...

When I enter '89.48', form is giving 'The value '89.48' is not valid for Price'. I think this is becuase of default language of my PC. It is not English. It is Russian. enter image description here

I tried to solve this issue by haacked.com instructions:

  1. I have created Model class DecimalModelBinder and copied code from haacked.com into class
  2. Updated Global.asax with

    ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());
    

    No effect. Then I tried to fix it by client-side validation 1.Added JavaScript file called "jQueryFixes.js" with code

$.validator.methods.range = function (value, element, param) {
    var globalizedValue = value.replace(",", ".");
    return this.optional(element) || (globalizedValue >= param[0] && globalizedValue <= param[1]);
}

$.validator.methods.number = function (value, element) {
    return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:[\s\.,]\d{3})+)(?:[\.,]\d+)?$/.test(value);
}

This code did not solve this issue. Can you suggest what I am doing wrong here?

Community
  • 1
  • 1
Joe Rakhimov
  • 4,713
  • 9
  • 51
  • 109

5 Answers5

6

I set in web.config

<system.web>
    <globalization uiCulture="en-US" culture="en-US"/>
<system.web>

this solution worked for me, i was getting the same error

Mehdi Benkirane
  • 437
  • 4
  • 7
1

Replace with this in your DecimalModelBinder class.

//if with period use InvariantCulture
if (valueResult.AttemptedValue.Contains("."))
{
    actualValue = Convert.ToDecimal(valueResult.AttemptedValue,
    CultureInfo.InvariantCulture);
}
else
{
    //if with comma use CurrentCulture
    actualValue = Convert.ToDecimal(valueResult.AttemptedValue,
    CultureInfo.CurrentCulture);
}

Solution comes from this link.

Community
  • 1
  • 1
MacGyver
  • 2,983
  • 1
  • 17
  • 14
0

try this

    [Required(ErrorMessage = "Price is required")]
    [Range(typeof(decimal), "0.01", "100000.00", ErrorMessage = "enter decimal value")]
    [RegularExpression(@"^\[0-9]{1,6}\.[0-9]{2}$", ErrorMessage="enter decimal value of format $9.99")]
    public decimal Price { get; set; }

Should help.

Ziaullah Khan
  • 2,020
  • 17
  • 20
0

At Global.asax file using below code is solved my problem for Turkish language

protected void Application_BeginRequest()
{
    var cultureInfo = new CultureInfo("tr-TR");
    cultureInfo.NumberFormat.NumberDecimalSeparator = ".";
    System.Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo;
    Thread.CurrentThread.CurrentCulture = cultureInfo;
}
-1

try this:

[Required(ErrorMessage = "Price is required")]
[DataType(DataType.Currency)]
public decimal Price { get; set; }    
Moslem Hadi
  • 846
  • 5
  • 18
  • 30
banny
  • 859
  • 7
  • 12
  • While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please [include an explanation for your code](//meta.stackexchange.com/q/114762/269535), as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.[From Review](http://stackoverflow.com/review/low-quality-posts/15990158) – Suraj Rao Apr 30 '17 at 13:41