1/* 2composed by Brian Jaeger 3home page: brianjaeger.com 4*/ 5(function ($) { 6 $.fn.limitkeypress = function (options) { 7 var defaults = { 8 rexp: /^[-+]?\d*\.?\d*$/ //only positive or negitive decimal numbers are allowed 9 }; 10 var options = $.extend(defaults, options); 11 12 return this.each(function() { 13 var regExpression = options.rexp; //get the regular expression 14 15 $(this).blur(function() { 16 //this fixes the problem of paste of invalid data then loss of focus ie clicked submit button or tab 17 sanitize(this); 18 }); 19 20 $(this).keypress(function(e) { 21 //allow backspace(8), enter(13), and other non character keypress events 22 if (e.which == "0" || e.which == "8" || e.which == "13" || e.ctrlKey || e.altKey){ 23 return; 24 } 25 26 //this fixes the problem of blur not triggering on enter keypress and it alows valid keypress events after an invalid paste/auto complete 27 sanitizeWithSelection(this); 28 29 var pressedChar = String.fromCharCode(e.which), //get string value for pressed char 30 //insert the pressed char at the caret/cursor location for testing 31 updatedInput = this.value.substring(0, getSelectionStart(this))+pressedChar+this.value.substring(getSelectionEnd(this), this.value.length); 32 if (!regExpression.test(updatedInput)) { 33 e.preventDefault(); //stop the keypress event 34 return; 35 } 36 return; 37 }); 38 39 //steps throu each char of a text input value validating each char + the next if the add is valid... 40 function sanitizeWithSelection(o) { 41 var startCaretPos = getSelectionStart(o), 42 endCaretPos = getSelectionEnd(o), 43 temp = "", 44 testPlusChar = "", 45 selectionCharInfo = []; 46 47 //records selection information for each char 48 for (i=0;i<o.value.length;i++) { 49 if (startCaretPos > i){ 50 selectionCharInfo[i] = 'beforeSelection'; 51 } else if ((startCaretPos <= i) && (endCaretPos > i)) { 52 selectionCharInfo[i] = 'inSelection'; 53 } //note: if a char after the selection is invalid the selection would not change if that char is removed... 54 } 55 56 for (i=0;i<o.value.length;i++) { 57 var iPlusOne = i + 1; 58 testPlusChar += o.value.substring(i,iPlusOne); 59 if ((!regExpression.test(testPlusChar))) { 60 var lastChar = testPlusChar.length-1; 61 temp = testPlusChar.substring(0,lastChar); 62 testPlusChar = temp; 63 if (selectionCharInfo[i] == 'beforeSelection'){ 64 startCaretPos = startCaretPos - 1; 65 endCaretPos = endCaretPos - 1; 66 } else if (selectionCharInfo[i] == 'inSelection'){ 67 endCaretPos = endCaretPos - 1; 68 } 69 } 70 } 71 o.value = testPlusChar; 72 setSelectionRange (o,startCaretPos,endCaretPos); 73 } 74 75 //steps throu each char of a text input value validating each char + the next if the add is valid... 76 function sanitize(o) { 77 var temp = "", 78 testPlusChar = ""; 79 80 for (i=0;i<o.value.length;i++) { 81 var iPlusOne = i+1; 82 testPlusChar += o.value.substring(i,iPlusOne); 83 if ((!regExpression.test(testPlusChar))) { 84 var lastChar = testPlusChar.length-1; 85 temp = testPlusChar.substring(0,lastChar); 86 testPlusChar = temp; 87 } 88 } 89 o.value = testPlusChar; 90 } 91 92 //from: http://javascript.nwbox.com/cursor_position/ 93 function getSelectionStart(o) { 94 if (o.createTextRange) { 95 var r = document.selection.createRange().duplicate() 96 r.moveEnd('character', o.value.length) 97 if (r.text == '') return o.value.length 98 return o.value.lastIndexOf(r.text) 99 } else return o.selectionStart 100 } 101 102 //from: http://javascript.nwbox.com/cursor_position/ 103 function getSelectionEnd(o) { 104 if (o.createTextRange) { 105 var r = document.selection.createRange().duplicate() 106 r.moveStart('character', -o.value.length) 107 return r.text.length 108 } else return o.selectionEnd 109 } 110 111 //from http://www.codingforums.com/archive/index.php/t-90176.html 112 function setSelectionRange(input, selectionStart, selectionEnd){ 113 if (input.setSelectionRange) { 114 input.focus(); 115 input.setSelectionRange(selectionStart, selectionEnd); 116 } 117 else if (input.createTextRange) { 118 var range = input.createTextRange(); 119 range.collapse(true); 120 range.moveEnd('character', selectionEnd); 121 range.moveStart('character', selectionStart); 122 range.select(); 123 } 124 } 125 });
126 }; 127})(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.