-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.js
36 lines (27 loc) · 975 Bytes
/
format.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/////////////////////////////////////////////////////////////////////////////////////
var currencyInput = document.querySelector('input[type="currency"]')
var currency = 'GBP' // https://www.currency-iso.org/dam/downloads/lists/list_one.xml
// format inital value
onkeyup({ target: currencyInput })
// bind event listeners
currencyInput.addEventListener('focus', onFocus)
currencyInput.addEventListener('keyup', onkeyup)
function localStringToNumber(s) {
return Number(String(s).replace(/[^0-9.-]+/g, ""))
}
function onFocus(e) {
var value = e.target.value;
e.target.value = value ? localStringToNumber(value) : ''
}
function onkeyup(e) {
var value = e.target.value
var options = {
maximumFractionDigits: 2,
currency: currency,
// style: "currency",
currencyDisplay: "symbol"
}
e.target.value = (value || value === 0) ?
localStringToNumber(value).toLocaleString(undefined, options) :
''
}