Created
October 30, 2012 21:34
-
-
Save fernandosavio/3983225 to your computer and use it in GitHub Desktop.
Plugin jQuery para formatação de inputs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the semi-colon before function invocation is a safety net against concatenated | |
// scripts and/or other plugins which may not be closed properly. | |
;(function ( $, window, undefined ) { | |
// undefined is used here as the undefined global variable in ECMAScript 3 is | |
// mutable (ie. it can be changed by someone else). undefined isn't really being | |
// passed in so we can ensure the value of it is truly undefined. In ES5, undefined | |
// can no longer be modified. | |
// window and document are passed through as local variables rather than globals | |
// as this (slightly) quickens the resolution process and can be more efficiently | |
// minified (especially when both are regularly referenced in your plugin). | |
// Create the defaults once | |
var pluginName = 'decimalFormat', | |
document = window.document, | |
defaults = { | |
decimal: '.', | |
thousand: ',' | |
}, | |
lPad = function(str, padString, length) { | |
while (str.length < length) {str = padString + str} | |
return str; | |
} | |
formatValor = function(valor){ | |
var result = [], i, n="" ; | |
if( isNaN(+valor) || !valor) return '0,00'; | |
if(valor < 0){ | |
valor *= -1; | |
n='-'; | |
} | |
valor = (+valor).toFixed(2).split('.'); | |
for(i = +valor[0] ; i ; i = Math.floor(i/1000)){ | |
result.unshift("" + lPad((i%1000)+"",'0' ,3 )); | |
} | |
result[0] = +result[0] + ""; | |
return n+result.join('.') + "," + valor[1]; | |
}; | |
// The actual plugin constructor | |
function Plugin( element, options ) { | |
this.element = element; | |
// jQuery has an extend method which merges the contents of two or | |
// more objects, storing the result in the first object. The first object | |
// is generally empty as we don't want to alter the default options for | |
// future instances of the plugin | |
this.options = $.extend( {}, defaults, options) ; | |
this._defaults = defaults; | |
this._name = pluginName; | |
this.init(); | |
} | |
Plugin.prototype.init = function () { | |
// Place initialization logic here | |
// You already have access to the DOM element and the options via the instance, | |
// e.g., this.element and this.options | |
var regex = /^[\d,\.]*$/i, | |
replaceRegex = /\D/g; | |
$(this.element).on('keydown', function(event){ | |
if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 || | |
// Allow: Ctrl+A | |
(event.keyCode == 65 && event.ctrlKey === true) || | |
// Allow: home, end, left, right | |
(event.keyCode >= 35 && event.keyCode <= 39)) { | |
// let it happen, don't do anything | |
return; | |
} | |
else { | |
// Ensure that it is a number and stop the keypress | |
if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) { | |
event.preventDefault(); | |
} | |
} | |
}); | |
$(this.element).on('change', function(event){ | |
this.value = formatValor(this.value.replace(replaceRegex, '')); | |
}); | |
}; | |
// A really lightweight plugin wrapper around the constructor, | |
// preventing against multiple instantiations | |
$.fn[pluginName] = function ( options ) { | |
return this.each(function () { | |
if (!$.data(this, 'plugin_' + pluginName)) { | |
$.data(this, 'plugin_' + pluginName, new Plugin( this, options )); | |
} | |
}); | |
} | |
}(jQuery, window)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment