ferrarisp

pto_virgula_maskMoney.js

Aug 25th, 2015
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * @Copyright (c) 2009 Aurélio Saraiva ([email protected])
  3. * @Page http://github.com/plentz/jquery-maskmoney
  4. * try at http://inovaideia.com.br/maskInputMoney/
  5.  
  6. * Special thanks to Raul Pereira da Silva ([email protected]) and Diego Plentz (http://plentz.org)
  7.  
  8. * Permission is hereby granted, free of charge, to any person
  9. * obtaining a copy of this software and associated documentation
  10. * files (the "Software"), to deal in the Software without
  11. * restriction, including without limitation the rights to use,
  12. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following
  15. * conditions:
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28.  
  29. /*
  30. * @Version: 0.3
  31. * @Release: 2009-10-29
  32. */
  33. (function($) {
  34. $.fn.maskMoney = function(settings) {
  35. settings = $.extend({
  36. symbol:'US$',
  37. decimal:'.',
  38. precision:2,
  39. thousands:',',
  40. allowZero:false,
  41. showSymbol:false
  42. }, settings);
  43.  
  44. settings.symbol=settings.symbol+' ';
  45.  
  46. return this.each(function() {
  47. var input = $(this);
  48.  
  49. function keypressEvent(e) {
  50. e = e||window.event;
  51. var k = e.charCode||e.keyCode||e.which;
  52.  
  53. if (k==8) { // tecla backspace
  54. preventDefault(e);
  55. var x = input.val().substring(0,input.val().length-1);
  56. input.val(maskValue(x));
  57. return false;
  58. } else if (k==9) { // tecla tab
  59. return true;
  60. }
  61. if (k<48||k>57) {
  62. preventDefault(e);
  63. return true;
  64. } else if (input.val().length==input.attr('maxlength')) {
  65. return false;
  66. }
  67.  
  68. var key = String.fromCharCode(k); // Valor para o código da Chave
  69. preventDefault(e);
  70. input.val(maskValue(input.val()+key));
  71. }
  72.  
  73. function focusEvent(e) {
  74. if (input.val()=='') {
  75. input.val(setSymbol(getDefaultMask()));
  76. } else {
  77. input.val(setSymbol(input.val()));
  78. }
  79.                 if (this.createTextRange) {
  80.                     var textRange = this.createTextRange();
  81.                     textRange.collapse(false); // posiciona cursor no final.
  82.                     textRange.select();
  83.                 }
  84. }
  85.  
  86. function blurEvent(e) {
  87.                 if ($.browser.msie) {
  88.                     keypressEvent(e);
  89.                 }
  90.  
  91. if (input.val()==setSymbol(getDefaultMask())) {
  92. if(!settings.allowZero) input.val('');
  93. } else {
  94. input.val(input.val().replace(settings.symbol,''));
  95. }
  96. }
  97.  
  98. function preventDefault(e) {
  99. if (e.preventDefault) { //standart browsers
  100. e.preventDefault();
  101. } else { // internet explorer
  102. e.returnValue = false
  103. }
  104. }
  105.  
  106. function maskValue(v) {
  107. v = v.replace(settings.symbol,'');
  108.  
  109. var strCheck = '0123456789';
  110. var len = v.length;
  111. var a = '', t = '';
  112.  
  113. if (len==0) {
  114. t = '0.00';
  115. }
  116.  
  117. for (var i = 0; i<len; i++) {
  118. if ((v.charAt(i)!='0') && (v.charAt(i)!=settings.decimal)) break;
  119.                 }
  120.  
  121. for (; i<len; i++) {
  122. if (strCheck.indexOf(v.charAt(i))!=-1) a+= v.charAt(i);
  123. }
  124.  
  125. var n = parseFloat(a);
  126. n = isNaN(n) ? 0 : n/Math.pow(10,settings.precision);
  127. t = n.toFixed(settings.precision);
  128.  
  129.                 i = settings.precision == 0 ? 0 : 1;
  130. var p, d = (t=t.split('.'))[i].substr(0,settings.precision);
  131. for (p = (t=t[0]).length; (p-=3)>=1;) {
  132. t = t.substr(0,p)+settings.thousands+t.substr(p);
  133. }
  134.  
  135. return (settings.precision>0)
  136.                     ? setSymbol(t+settings.decimal+d+Array((settings.precision+1)-d.length).join(0))
  137.                     : setSymbol(t);
  138. }
  139.  
  140. function getDefaultMask() {
  141. var n = parseFloat('0')/Math.pow(10,settings.precision);
  142. return (n.toFixed(settings.precision)).replace(new RegExp('\\.','g'),settings.decimal);
  143. }
  144.  
  145. function setSymbol(v) {
  146. if (settings.showSymbol) {
  147. return settings.symbol+v;
  148. }
  149. return v;
  150. }
  151.  
  152. input.bind('keypress',keypressEvent);
  153. input.bind('blur',blurEvent);
  154. input.bind('focus',focusEvent);
  155.  
  156. input.one('unmaskMoney',function() {
  157. input.unbind('focus',focusEvent);
  158. input.unbind('blur',blurEvent);
  159. input.unbind('keypress',keypressEvent);
  160.  
  161. if ($.browser.msie) {
  162.                     this.onpaste= null;
  163. } else if ($.browser.mozilla) {
  164.                     this.removeEventListener('input',blurEvent,false);
  165.                 }
  166. });
  167. });
  168. }
  169.  
  170. $.fn.unmaskMoney=function() {
  171. return this.trigger('unmaskMoney');
  172. };
  173. })(jQuery);
Add Comment
Please, Sign In to add comment