jquery.price_format.1.2.js
3.41 KB
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
/*
* Price Format jQuery Plugin
* By Eduardo Cuducos
* cuducos [at] gmail [dot] com
* Version: 1.1
* Release: 2009-02-10
* original char limit by Flávio Silveira <http://flaviosilveira.com>
* original keydown event attachment by Kaihua Qi
*/
(function($) {
$.fn.priceFormat = function(options) {
var defaults = {
prefix: 'US$ ',
centsSeparator: '.',
thousandsSeparator: ',',
limit: false,
centsLimit: 2
};
var options = $.extend(defaults, options);
return this.each(function() {
// pre defined options
var obj = $(this);
var is_number = /[0-9]/;
// load the pluggings settings
var prefix = options.prefix;
var centsSeparator = options.centsSeparator;
var thousandsSeparator = options.thousandsSeparator;
var limit = options.limit;
var centsLimit = options.centsLimit;
// skip everything that isn't a number
// and also skip the left zeroes
function to_numbers (str) {
var formatted = '';
for (var i=0;i<(str.length);i++) {
wchar = str.charAt(i);
if (formatted.length==0 && wchar==0) wchar = false;
if (wchar && wchar.match(is_number)) {
if (limit) {
if (formatted.length < limit) formatted = formatted+wchar;
}else{
formatted = formatted+wchar;
}
}
}
return formatted;
}
// format to fill with zeros to complete cents chars
function fill_with_zeroes (str) {
while (str.length<(centsLimit+1)) str = '0'+str;
return str;
}
// format as price
function price_format (str) {
// formatting settings
var formatted = fill_with_zeroes(to_numbers(str));
var thousandsFormatted = '';
var thousandsCount = 0;
// split integer from cents
var centsVal = formatted.substr(formatted.length-centsLimit,centsLimit);
var integerVal = formatted.substr(0,formatted.length-centsLimit);
// apply cents pontuation
formatted = integerVal+centsSeparator+centsVal;
// apply thousands pontuation
if (thousandsSeparator) {
for (var j=integerVal.length;j>0;j--) {
wchar = integerVal.substr(j-1,1);
thousandsCount++;
if (thousandsCount%3==0) wchar = thousandsSeparator+wchar;
thousandsFormatted = wchar+thousandsFormatted;
}
if (thousandsFormatted.substr(0,1)==thousandsSeparator) thousandsFormatted = thousandsFormatted.substring(1,thousandsFormatted.length);
formatted = thousandsFormatted+centsSeparator+centsVal;
}
// apply the prefix
if (prefix) formatted = prefix+formatted;
return formatted;
}
// filter what user type (only numbers and functional keys)
function key_check (e) {
var code = (e.keyCode ? e.keyCode : e.which);
var typed = String.fromCharCode(code);
var functional = false;
var str = obj.val();
var newValue = price_format(str+typed);
// check Backspace, Tab, Enter
if (code == 8) functional = true;
if (code == 9) functional = true;
if (code == 13) functional = true;
if (!functional) {
e.preventDefault();
e.stopPropagation();
if (str!=newValue) obj.val(newValue);
}
}
// inster formatted price as a value of an input field
function price_it () {
var str = obj.val();
var price = price_format(str);
if (str != price) obj.val(price);
}
// bind the actions
//$(this).bind('keydown', key_check);
$(this).bind('keyup', price_it);
if ($(this).val().length>0) price_it();
});
};
})(jQuery);