如何通过区域设置验证货币价值?
How to validate currency value by Locale?
我们有一个大型网络应用程序,在不同国家/地区都有门户。
在这些国家/地区,货币价值的插入方式不同。
是否存在可以通过区域设置验证货币价值的解决方案?
您可以在货币文本框旁边提供一个下拉菜单。所以用户可以 select 适当的货币并可以填写值。
并且在后端你需要获得这两个值。用户输入的值和货币 selected。
根据位置,您可以更改默认的 selected 货币。
"We have a big web application, that has portals in different countries"
我强烈建议使用 Globalize 设置本地化。正确设置后,您所要做的就是更改用户的 locale
,所有与区域设置相关的格式(例如货币、日期、数字、单位等)都会自行修复。
如果您只想将其用于货币,请参阅他们的货币模块:Globalize Currency Module
我会推荐 http://openexchangerates.github.io/money.js/ 这将为您提供实时货币兑换率。
出于格式化目的,请查看 AccountingJs https://josscrowcroft.github.io/accounting.js/
我现在已经通过自己的函数实现了它:
/*
* checks if a given money value is a correct one via locale language
* */
function isMoney(moneyValue) {
var currentSelectedLanguage = $('#currentSelectedLanguage').val();
switch (currentSelectedLanguage)
{
// checks for , as separator and . as decimal separator e.g.: 1,234,567,890.98 - also allows without , as separator e.g.: 1234567890.98
case "en":
return /(?=.)^$?(([1-9][0-9]{0,2}(,[0-9]{3})*)|[0-9]+)?(\.[0-9]{1,2})?$/.test(moneyValue);
break;
// checks for . as separator and , as decimal separator e.g.: 1.234.567.890,98 - also allows without . as separator e.g.: 1234567890,98
case "de":
case "el":
case "es":
case "hr":
case "ro":
case "sl":
case "sr":
return /(?=.)^$?(([1-9][0-9]{0,2}(\.[0-9]{3})*)|[0-9]+)?(,[0-9]{1,2})?$/.test(moneyValue);
break;
// checks for space as separator and , as decimal separator e.g.: 1 234 567 890,98, also allows without space as separator e.g.: 1234567890,98
case "bg":
case "cs":
case "hu":
case "pl":
case "ru":
case "sk":
case "uk":
return /(?=.)^$?(([1-9][0-9]{0,2}( [0-9]{3})*)|[0-9]+)?(,[0-9]{1,2})?$/.test(moneyValue);
break;
// checks for . as separator and , as decimal separator e.g.: 1.234.567.890,98 - also allows without . as separator e.g.: 1234567890,98
default:
return /(?=.)^$?(([1-9][0-9]{0,2}(\.[0-9]{3})*)|[0-9]+)?(,[0-9]{1,2})?$/.test(moneyValue);
}
}
唯一的缺点是,如果我们的应用程序中有新的语言环境,我必须自己扩展它
我们有一个大型网络应用程序,在不同国家/地区都有门户。 在这些国家/地区,货币价值的插入方式不同。
是否存在可以通过区域设置验证货币价值的解决方案?
您可以在货币文本框旁边提供一个下拉菜单。所以用户可以 select 适当的货币并可以填写值。
并且在后端你需要获得这两个值。用户输入的值和货币 selected。
根据位置,您可以更改默认的 selected 货币。
"We have a big web application, that has portals in different countries"
我强烈建议使用 Globalize 设置本地化。正确设置后,您所要做的就是更改用户的 locale
,所有与区域设置相关的格式(例如货币、日期、数字、单位等)都会自行修复。
如果您只想将其用于货币,请参阅他们的货币模块:Globalize Currency Module
我会推荐 http://openexchangerates.github.io/money.js/ 这将为您提供实时货币兑换率。
出于格式化目的,请查看 AccountingJs https://josscrowcroft.github.io/accounting.js/
我现在已经通过自己的函数实现了它:
/*
* checks if a given money value is a correct one via locale language
* */
function isMoney(moneyValue) {
var currentSelectedLanguage = $('#currentSelectedLanguage').val();
switch (currentSelectedLanguage)
{
// checks for , as separator and . as decimal separator e.g.: 1,234,567,890.98 - also allows without , as separator e.g.: 1234567890.98
case "en":
return /(?=.)^$?(([1-9][0-9]{0,2}(,[0-9]{3})*)|[0-9]+)?(\.[0-9]{1,2})?$/.test(moneyValue);
break;
// checks for . as separator and , as decimal separator e.g.: 1.234.567.890,98 - also allows without . as separator e.g.: 1234567890,98
case "de":
case "el":
case "es":
case "hr":
case "ro":
case "sl":
case "sr":
return /(?=.)^$?(([1-9][0-9]{0,2}(\.[0-9]{3})*)|[0-9]+)?(,[0-9]{1,2})?$/.test(moneyValue);
break;
// checks for space as separator and , as decimal separator e.g.: 1 234 567 890,98, also allows without space as separator e.g.: 1234567890,98
case "bg":
case "cs":
case "hu":
case "pl":
case "ru":
case "sk":
case "uk":
return /(?=.)^$?(([1-9][0-9]{0,2}( [0-9]{3})*)|[0-9]+)?(,[0-9]{1,2})?$/.test(moneyValue);
break;
// checks for . as separator and , as decimal separator e.g.: 1.234.567.890,98 - also allows without . as separator e.g.: 1234567890,98
default:
return /(?=.)^$?(([1-9][0-9]{0,2}(\.[0-9]{3})*)|[0-9]+)?(,[0-9]{1,2})?$/.test(moneyValue);
}
}
唯一的缺点是,如果我们的应用程序中有新的语言环境,我必须自己扩展它