0

So I currently developing a website that support many languages. I have an input box where user can input the amount of currency inside. I need a function to validate if that input is legit or not. however, because different countries use different format of number. for example: England use '.' for decimal and ',' for thousand separator . Where as Germany use ',' for decimal and '.' for thousand separator. French use ',' for decimal and (space) for thousand separator... And for Chinese/Jap , they even dont use number "1-9" to describe number

I can make a very big if-else function to do the validate base on the language they are using. something like this

number = userinput()
if "de":
return deValidator(number)
if "fr":
return frValidator(number)
if "en":
return enValidator(number)
if "zh":
return zhValidator(number)

However, is there any wiser way to do it?? what I am looking for is something like a already-built validator/library or an easier approach to solve this problem without having to writing different validator for different language

Jake Lam
  • 3,254
  • 3
  • 25
  • 44
  • 1
    What about momentjs use?Did u tried your option with this? – RIYAJ KHAN Jun 04 '18 at 07:30
  • @RIYAJKHAN I thought momentjs only dealing with date and time ?? Please correct me if i wrong – Jake Lam Jun 04 '18 at 07:31
  • Possible duplicate of [Currency validation](https://stackoverflow.com/questions/2227370/currency-validation) – Johnson Samuel Jun 04 '18 at 07:43
  • @johnsam i dont think so , because that question is only for english format, i am asking a wise way to do with multiple format for different countries in the word – Jake Lam Jun 04 '18 at 07:50
  • how about simple expression return lang+'Validator' – Fadi Abo Msalam Jun 04 '18 at 07:55
  • @FadiAboMsalam i think you get my question wrong, I am looking for something like a built in validator or an easier approach to solve this problem without having to writing different validator for different language – Jake Lam Jun 04 '18 at 07:57
  • this looks quite neat https://www.npmjs.com/package/react-currency-format – Fadi Abo Msalam Jun 04 '18 at 08:00
  • i also thinks you need to implement a small function that takes a language as input as return the "thousandSeparator" and "decimalSeparator" from the package above – Fadi Abo Msalam Jun 04 '18 at 08:02
  • @NgocTuanLam: You should check http://openexchangerates.github.io/accounting.js/ – Johnson Samuel Jun 04 '18 at 08:03
  • why not use `switch/case`, it is much faster – Jee Mok Jun 04 '18 at 08:42
  • I don't think there is this library out there yet, maybe you can create one library :) it sound pretty useful – Jee Mok Jun 04 '18 at 08:42
  • @JeeMok yes switch/case can archive the same thing but still need to write the validator for each language. Yeah i also cannot find any libraries or any tricks to archive this – Jake Lam Jun 04 '18 at 08:55
  • @johnsam thanks for your recommendation , however I think this only can do things like take the number and display it correctly. They still yet cannot do things like validator: What I want is something like if I input "1.2" in French language , it must return NaN – Jake Lam Jun 04 '18 at 08:56
  • @FadiAboMsalam thanks for your recommendation too , but I think this library also cannot do my problem just like of john – Jake Lam Jun 04 '18 at 08:56

1 Answers1

0

You can leverage on toLocaleString() method to help to build a validator; The toLocaleString() method returns a string with a language sensitive representation of the number.

const number = 123456.789;

// German uses comma as decimal separator and period for thousands
console.log(number.toLocaleString('de-DE'));
// → 123.456,789

// Arabic in most Arabic speaking countries uses Eastern Arabic digits
console.log(number.toLocaleString('ar-EG'));
// → ١٢٣٤٥٦٫٧٨٩

// India uses thousands/lakh/crore separators
console.log(number.toLocaleString('en-IN'));
// → 1,23,456.789

// the nu extension key requests a numbering system, e.g. Chinese decimal
console.log(number.toLocaleString('zh-Hans-CN-u-nu-hanidec'));
// → 一二三,四五六.七八九

// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
console.log(number.toLocaleString(['ban', 'id']));
// → 123.456,789

With this method, you can also format numbers with currency information:

const number = 10000000;

number.toLocaleString('it-IT', {style: 'currency', currency: 'EUR'})
// → 10.000.000,00 €

number.toLocaleString('it-IT', {style: 'currency', currency: 'USD'})
// → 10.000.000,00 US$

number.toLocaleString('en-US', {style: 'currency', currency: 'EUR'})
// → €10,000,000.00

number.toLocaleString('en-US', {style: 'currency', currency: 'USD'})
// → $10,000,000.00

For more details: toLocaleString https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

Jee Mok
  • 6,157
  • 8
  • 47
  • 80