/* composed by Brian Jaeger home page: brianjaeger.com */ (function ($) { $.fn.limitkeypress = function (options) { var defaults = { rexp: /^[-+]?\d*\.?\d*$/ //only positive or negitive decimal numbers are allowed }; var options = $.extend(defaults, options); return this.each(function() { var regExpression = options.rexp; //get the regular expression $(this).blur(function() { //this fixes the problem of paste of invalid data then loss of focus ie clicked submit button or tab sanitize(this); }); $(this).keypress(function(e) { //alow backspace(8), enter(13), and other non character keypress events if (e.which == "0" || e.which == "8" || e.which == "13" || e.ctrlKey || e.altKey){ return; } //this fixes the problem of blur not triggering on enter keypress and it alows valid keypress events after an invalid paste/auto complete sanitizeWithSelection(this); var pressedChar = String.fromCharCode(e.which), //get string value for pressed char //insert the pressed char at the caret/cursor location for testing updatedInput = this.value.substring(0, getSelectionStart(this)) + pressedChar + this.value.substring(getSelectionEnd(this), this.value.length); if (!regExpression.test(updatedInput)) { e.preventDefault(); //stop the keypress event return; } return; }); //steps throu each char of a text input value validating each char + the next if the add is valid... function sanitizeWithSelection(o) { var startCaretPos = getSelectionStart(o), endCaretPos = getSelectionEnd(o), temp = "", testPlusChar = "", selectionCharInfo = []; //records selection information for each char for (i=0;i i){ selectionCharInfo[i] = 'beforeSelection'; } else if ((startCaretPos <= i) && (endCaretPos > i)) { selectionCharInfo[i] = 'inSelection'; } //note: if a char after the selection is invalid the selection would not change if that char is removed... } for (i=0;i