Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Web Config

<globalization uiCulture="en-US" culture="en-US" />


Entity

[DisplayName("Price")]
public double Price { get; set; }


Create.cshtml

<div class="form-group">
        @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
        </div>
    </div>


What I have tried:

Problem

I tried to figure it out using regular expression, but I couldn't.

I get this error when I add the product price.How can I fix? Thank you.
Posted
Updated 22-Jan-18 23:17pm
v2
Comments
[no name] 23-Jan-18 4:26am    
Please describe where you are getting the issue and also please write the complete code where you need a fix.
F-ES Sitecore 23-Jan-18 4:31am    
You're trying to set price to "1.500, 10" which isn't a valid value, that text can't be converted to a double. It looks like you have globalisation issues and someone is entering a price for a country where that format is valid, but it's not valid for the locale you're using (US). The solution is to enter prices in the correct format for the locale.
Member 13582084 23-Jan-18 4:37am    
i can try de-DE but same problem.
F-ES Sitecore 23-Jan-18 4:47am    
Look at the global.asax code in the question in this thread, you can ignore the rest

https://stackoverflow.com/questions/32586551/what-is-the-best-way-to-handle-validation-with-different-culture
[no name] 23-Jan-18 4:45am    
You are right F-ES .Either restrict not to enter any characters and only integers /demials are required.

1 solution

Tell the user to enter values without thousands separator or remove it before trying to convert numeric strings to numbers.

Another problem is that parseFloat() accepts only numeric strings with the period as decimal point. So it might be necessary to replace other decimal points like the comma.

Because both problems are related, supporting input strings with thousands separators is quite difficult.

The simple solution (input must be without thousands separator)
JavaScript
priceEN = price.replace(/[,\u2396\u066B]/g, '.');
priceValue = parseFloat(priceEN);
This will replace other decimal points with the period.

The advanced solution (requires defining or determining a locale)
Get the thousands separator from the locale and remove it from the string. Example for the period (locales using comma as decimal point):
JavaScript
priceLoc = price.replace(/[^0-9-,]/g, ''));
For locales using period as decimal point
JavaScript
priceLoc = price.replace(/[^0-9-.]/g, ''));
The above will also remove other characters like currency symbols. If the decimal point is not the period, pass the resulting string to the regex from the simple example before calling parseFloat().
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900