Created
June 27, 2011 18:17
-
-
Save pielgrzym/1049417 to your computer and use it in GitHub Desktop.
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
(function($){ | |
$.fn.extend({ | |
formatInput: function(custom_settings) { | |
settings = $.extend({ | |
regex:".*", | |
format_number: true, | |
max_length: false, | |
errback: null | |
}, custom_settings); | |
return this.each(function() { | |
var elem = $(this); | |
if(settings.format_number == true) | |
{ | |
elem.bind("keyup.format_number", $.fn.formatEvent); | |
} | |
elem.bind("keypress.filter_input", $.fn.filterEvent); | |
}); | |
}, | |
filterEvent: function(e) { | |
var s = settings; | |
var regex = new RegExp(s.regex); | |
var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0; | |
var string = String.fromCharCode(key); | |
var elem = $(this); | |
if (key == 8 || key == 9 || key == 13 || key == 35 || key == 36|| key == 37 || key == 39 || key == 46) { | |
if ($.browser.mozilla) { | |
// if charCode = key & keyCode = 0 | |
// 35 = #, 36 = $, 37 = %, 39 = ', 46 = . | |
if (event.charCode == 0 && event.keyCode == key) { | |
return true; | |
} | |
} | |
} | |
if(s.max_length != false) | |
{ | |
var value = elem.val(); | |
if(value != undefined && value.length >= s.max_length) | |
{ | |
return false; | |
} | |
} | |
if(regex.test(string)) | |
{ | |
return true; | |
} else { | |
return false; | |
} | |
}, | |
formatEvent: function(e) { | |
var elem = $(this); | |
var initial_value = elem.val().replace(" ", ""); | |
elem.val($.fn.insertSpaces(initial_value)); | |
}, | |
insertSpaces: function(number) { | |
return number.replace(/(\d)(?=(?:\d{3})+$)/g, "$1 "); | |
} | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment