PageSourceSearch

https://octopus.ffsv.ch/Scripts/currency-format.js

js ffsv.ch collected 2026-10-08 11:17:53 UTC 2,814 bytes, 105 lines download raw bytes

1$(document).ready(function () {
2
3    if ($('.c-money')[0]) {
4        $('.c-money').each(function (index) {
5            console.log(index + ": " + $(this).val());
6            formatCurrency($(this));
7        });
8    }
9
10    $('.c-money').on({
11        keyup: function () {
12            formatCurrency($(this));
13        }
14    });
15});
16
17function formatNumber(n) {
18    // format number 1000000 to 1,234,567
19    var val = n.replace(/\D/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, "'");
20    if (n.startsWith("-")) {
21        val = "-" + val;
22    }
23    return val;
24}
25
26
27function formatCurrency(input, blur) {
28    // appends $ to value, validates decimal side
29    // and puts cursor back in right position.
30
31    // get input value
32    var input_val = input.val();
33
34    // don't validate empty input
35    if (input_val === "") { return; }
36
37    // original length
38    var original_len = input_val.length;
39
40    // initial caret position 
41    var caret_pos = input.prop("selectionStart");
42
43    // check for decimal
44    if (input_val.indexOf(".") >= 0) {
45
46        // get position of first decimal
47        // this prevents multiple decimals from
48        // being entered
49        var decimal_pos = input_val.indexOf(".");
50
51        // split number by decimal point
52        var left_side = input_val.substring(0, decimal_pos);
53        var right_side = input_val.substring(decimal_pos);
54
55        // add commas to left side of number
56        left_side = formatNumber(left_side);
57
58        // validate right side
59        right_side = formatNumber(right_side);
60
61
62        // Limit decimal to only 2 digits
63        right_side = right_side.substring(0, 2);
64
65        // join number by .
66        input_val = left_side + "." + right_side;
67
68    } else {
69        // no decimal entered
70        // add commas to number
71        // remove all non-digits
72        input_val = formatNumber(input_val);
73
74    }
75
76    // send updated string to input
77    input.val(input_val);
78
79    // put caret back in the right position
80    var updated_len = input_val.length;
81    caret_pos = updated_len - original_len + caret_pos;
82    input[0].setSelectionRange(caret_pos, caret_pos);
83}
84
85function formatDecimal(n) {
86    n = n + "";
87    if (n.indexOf(".") >= 0) {
88        var decimal_pos = n.indexOf(".");
89        // split number by decimal point
90        var left_side = n.substring(0, decimal_pos);
91        var right_side = n.substring(decimal_pos);
92
93        var left_side = formatNumber(left_side);
94        var right_side = formatNumber(right_side);
95
96        // Limit decimal to only 2 digits
97        right_side = right_side.substring(0, 2);
98
99        // join number by .
100        return left_side + "." + right_side;
101    }
102    else {
103        return formatNumber(n);
104    }
105}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.