I am trying to check a decimal field using globalization.
I need my value to be in French format (with a coma as the decimal separator and without the thousands separator).
EDIT : updated after. This is a great solution.
So here is my model:
public class CompanyTaxHistoryModel
{
[Required]
public DateTime DateFrom { get; set; }
[Required]
public DateTime DateTo { get; set; }
[Required]
[Range(0, 100, ErrorMessage = "The percentage must be between 0 and 100")]
[DisplayFormat(DataFormatString = "{0:N} %")]
[Display(Name = "Company Tax")]
public decimal CompanyTaxPercent { get; set; }
}
here is my web.config:
<globalization requestEncoding="UTF-8" responseEncoding="UTF-8" culture="auto" uiCulture="auto" / >
I added "localizationHelper" as follows:
namespace xxxx.Helpers
{
public static class LocalizationHelpers
{
public static IHtmlString MetaAcceptLanguage<t>(this HtmlHelper<t> html)
{
var acceptLanguage = HttpUtility.HtmlAttributeEncode(System.Threading.Thread.CurrentThread.CurrentUICulture.ToString());
return new HtmlString(String.Format("<meta name=\"accept-language\" content=\" {0}\">",acceptLanguage));
}
}
}
I use in my _Layout.cshtml:
@using xxxx.Helpers
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
@Html.MetaAcceptLanguage()
I added Globalize.js + the necessary cultures to my project and added below to my scripts:
$(document).ready(function () {
var data = $("meta[name='accept-language']").attr("content")
Globalize.culture(data);
});
$.validator.methods.date = function (value, element) {
return Globalize.parseDate(value);
};
$.validator.methods.range = function (value, element, param) {
return this.optional(element) || (Globalize.parseFloat(value) >= param[0] && Globalize.parseFloat(value) <= param[1]);
}
$.validator.methods.number = function (value, element) {
return !isNaN(Globalize.parseFloat(value));
}
Everything seems to be working fine, but I have a problem with decimal values like "." the button on any keyboard should display the assigned value (either "." or "," according to local culture).
, fr-FR, "12,5" , "12,5", .
"." .
, ?