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