forked from price-format/Jquery-Price-Format
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Prototype_Price_Format.js
175 lines (151 loc) · 4.26 KB
/
Prototype_Price_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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
NumberFormat = Class.create( {
initialize : function(el, options) {
this.el = $(el);
var options = Object.extend( {
prefix : 'US$ ',
centsSeparator : '.',
thousandsSeparator : ',',
limit : false,
centsLimit : 2,
clearPrefix : false,
allowNegative : false,
clearOnEmpty: false
}, options);
this.is_number = /[0-9]/;
// load the pluggings settings
this.prefix = options.prefix;
this.centsSeparator = options.centsSeparator;
this.thousandsSeparator = options.thousandsSeparator;
this.limit = options.limit;
this.centsLimit = options.centsLimit;
this.clearPrefix = options.clearPrefix;
this.allowNegative = options.allowNegative;
this.clearOnEmpty = options.clearOnEmpty;
if (this.clearPrefix) {
Event.observe(this.el, 'blur', this.clearPrefix.bind(this));
Event.observe(this.el, 'focus', this.addPrefix.bind(this));
}
Event.observe(this.el, 'keyup', this.priceIt.bind(this));
Event.observe(this.el, 'keydown', this.keyCheck.bind(this));
if (this.el.value.length > 0) {
this.priceIt();
this.clearPrefix();
}
},
keyCheck : function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
var typed = String.fromCharCode(code);
var functional = false;
var str = e.target.value;
var newValue = this.priceFormat(str + typed);
// allow key numbers, 0 to 9
if ((code >= 48 && code <= 57) || (code >= 96 && code <= 105))
functional = true;
// check Backspace, Tab, Enter, Delete, and left/right arrows
if (code == 8)
functional = true;
if (code == 9)
functional = true;
if (code == 13)
functional = true;
if (code == 46)
functional = true;
if (code == 35)
functional = true;
if (code == 36)
functional = true;
if (code == 37)
functional = true;
if (code == 39)
functional = true;
if (this.allowNegative && (code == 189 || code == 109))
functional = true; // dash as well
if (!functional) {
e.preventDefault();
e.stopPropagation();
if (str != newValue)
e.target.value = newValue;
}
},
toNumbers : function(str) {
var formatted = '';
for ( var i = 0; i < (str.length); i++) {
char = str.charAt(i);
if (formatted.length == 0 && char == 0)
char = false;
if (char && char.match(this.is_number)) {
if (this.limit) {
if (formatted.length < this.limit)
formatted = formatted + char;
} else {
formatted = formatted + char;
}
}
}
return formatted;
},
priceFormat : function(str, ignore) {
if(!ignore && (str === '' || str == this.priceFormat('0', true)) && clearOnEmpty) {
return '';
}
// formatting settings
var formatted = this.fillWithZeroes(this.toNumbers(str));
var thousandsFormatted = '';
var thousandsCount = 0;
// split integer from cents
var centsVal = formatted.substr(formatted.length - this.centsLimit,
this.centsLimit);
var integerVal = formatted.substr(0, formatted.length - this.centsLimit);
// apply cents pontuation
formatted = integerVal + this.centsSeparator + centsVal;
// apply thousands pontuation
if (this.thousandsSeparator) {
for ( var j = integerVal.length; j > 0; j--) {
char = integerVal.substr(j - 1, 1);
thousandsCount++;
if (thousandsCount % 3 == 0)
char = this.thousandsSeparator + char;
thousandsFormatted = char + thousandsFormatted;
}
if (thousandsFormatted.substr(0, 1) == this.thousandsSeparator)
thousandsFormatted = thousandsFormatted.substring(1,
thousandsFormatted.length);
formatted = thousandsFormatted + this.centsSeparator + centsVal;
}
// if the string contains a dash, it is negative - add it to the
// begining (except for zero)
if (this.allowNegative && str.indexOf('-') != -1
&& (integerVal != 0 || centsVal != 0))
formatted = '-' + formatted;
// apply the prefix
if (this.prefix) {
formatted = this.prefix + formatted;
}
return formatted;
},
fillWithZeroes : function(str) {
while (str.length < (this.centsLimit + 1)) {
str = '0' + str;
}
return str;
},
priceIt : function() {
var str = this.el.value;
var price = this.priceFormat(str);
if (str != price) {
this.el.value = price;
}
},
// Add prefix on focus
addPrefix : function() {
var val = el.value;
el.value = this.prefix + val;
},
// Clear prefix on blur if is set to true
clearPrefix : function() {
if (prefix.trim() != '' && this.clearPrefix) {
var array = el.value.split(this.prefix);
el.value = array[1];
}
}
});