occurred
occurred

Reputation: 508

How to validate currency value by Locale?

We have a big web application, that has portals in different countries. In these countries the money value is inserted differently.

Exists a solution which can validate the money value by locale?

Upvotes: 0

Views: 1318

Answers (4)

occurred
occurred

Reputation: 508

I've implemented it now via an own function:

/*
* 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);
    }

}

Only downside is, that I've to extend it on my own if there is a new locale in our application

Upvotes: 0

abhirathore2006
abhirathore2006

Reputation: 3745

i would recommend the http://openexchangerates.github.io/money.js/ this would provide you the live currency conversion rates.

for the formatting purpose look into AccountingJs https://josscrowcroft.github.io/accounting.js/

Upvotes: 1

Ozan
Ozan

Reputation: 3749

"We have a big web application, that has portals in different countries"

I highly recommend setting up localization with Globalize. After setting it up correctly, all you have to do is change the locale of the user and all the locale dependent formatting (e.g currency, date, number, units, etc.) fixes itself.

If you want to use it just for currency, see their currency module: Globalize Currency Module

Upvotes: 0

Krupall
Krupall

Reputation: 409

you can provide a dropdown next to the currency text box. so user can select the approperiate currency and can fill the value.

and in backend you need to get both value. entered value by user and the currecy selected.

based on location, you can change the selected currency by default.

Upvotes: 0

Related Questions