-
Notifications
You must be signed in to change notification settings - Fork 1
/
curFormatter.js
59 lines (52 loc) · 1.92 KB
/
curFormatter.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var curFormatter = function(){
this.formatRupiah = function(value, prefix){
let new_number = value.replace(/[^,\d]/g, '').toString(),
split = new_number.split(','),
remains = split[0].length % 3,
rupiah = split[0].substr(0, remains),
thousands = split[0].substr(remains).match(/\d{3}/gi);
if(thousands){
separator = remains ? '.' : '';
rupiah = separator thousands.join('.');
}
rupiah = split[1] != undefined ? rupiah ',' split[1] : rupiah;
return prefix == undefined ? rupiah : (rupiah ? prefix rupiah : '');
}
this.unformatRupiah = function(value){
let new_number = value.replace(/[^,\d]/g, '').toString().replace('.', '').replace(',', '.');
return new_number; /*NB: don't reformat this value if decimal number*/
}
this.inputOri = function(ele_ori, value){
let parent = this;
if(typeof ele_ori == "object")
{
ele_ori.value = parent.unformatRupiah(value);
}
if(typeof ele_ori == "string")
{
let inputs = document.querySelectorAll(ele_ori);
[].forEach.call(inputs,function(input){
parent.inputOri(input, value);
});
}
}
this.input = function(ele, prefix, ele_ori){
let parent = this;
if(typeof ele == "object")
{
ele.value = parent.formatRupiah(ele.value, prefix);
parent.inputOri(ele_ori, ele.value);
ele.addEventListener('keyup',function(){
ele.value = parent.formatRupiah(ele.value, prefix);
parent.inputOri(ele_ori, ele.value);
});
}
if(typeof ele == "string")
{
let inputs = document.querySelectorAll(ele);
[].forEach.call(inputs,function(input){
parent.input(input, prefix, ele_ori);
});
}
}
}