Regex.js 4.2 KB

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