/ kosimpin/assets/js/jquery/plugins/jquery.numeric.js

http://kosimpin.googlecode.com/ · JavaScript · 135 lines · 95 code · 1 blank · 39 comment · 80 complexity · 7488ddbe4c096dc70c477b770bee7326 MD5 · raw file

  1. /*
  2. *
  3. * Copyright (c) 2006-2010 Sam Collett (http://www.texotela.co.uk)
  4. * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
  5. * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
  6. *
  7. * Version 1.1.1
  8. * Demo: http://www.texotela.co.uk/code/jquery/numeric/
  9. *
  10. */
  11. (function($) {
  12. /*
  13. * Allows only valid characters to be entered into input boxes.
  14. * Note: does not validate that the final text is a valid number
  15. * (that could be done by another script, or server-side)
  16. *
  17. * @name numeric
  18. * @param decimal Decimal separator (e.g. '.' or ',' - default is '.')
  19. * @param callback A function that runs if the number is not valid (fires onblur)
  20. * @author Sam Collett (http://www.texotela.co.uk)
  21. * @example $(".numeric").numeric();
  22. * @example $(".numeric").numeric(",");
  23. * @example $(".numeric").numeric(null, callback);
  24. *
  25. */
  26. $.fn.numeric = function(decimal, callback)
  27. {
  28. decimal = decimal || ".";
  29. callback = typeof callback == "function" ? callback : function(){};
  30. this.keypress(
  31. function(e)
  32. {
  33. var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
  34. // allow enter/return key (only when in an input box)
  35. if(key == 13 && this.nodeName.toLowerCase() == "input")
  36. {
  37. return true;
  38. }
  39. else if(key == 13)
  40. {
  41. return false;
  42. }
  43. var allow = false;
  44. // allow Ctrl+A
  45. if((e.ctrlKey && key == 97 /* firefox */) || (e.ctrlKey && key == 65) /* opera */) return true;
  46. // allow Ctrl+X (cut)
  47. if((e.ctrlKey && key == 120 /* firefox */) || (e.ctrlKey && key == 88) /* opera */) return true;
  48. // allow Ctrl+C (copy)
  49. if((e.ctrlKey && key == 99 /* firefox */) || (e.ctrlKey && key == 67) /* opera */) return true;
  50. // allow Ctrl+Z (undo)
  51. if((e.ctrlKey && key == 122 /* firefox */) || (e.ctrlKey && key == 90) /* opera */) return true;
  52. // allow or deny Ctrl+V (paste), Shift+Ins
  53. if((e.ctrlKey && key == 118 /* firefox */) || (e.ctrlKey && key == 86) /* opera */
  54. || (e.shiftKey && key == 45)) return true;
  55. // if a number was not pressed
  56. if(key < 48 || key > 57)
  57. {
  58. /* '-' only allowed at start */
  59. if(key == 45 && this.value.length == 0) return true;
  60. /* only one decimal separator allowed */
  61. if(key == decimal.charCodeAt(0) && this.value.indexOf(decimal) != -1)
  62. {
  63. allow = false;
  64. }
  65. // check for other keys that have special purposes
  66. if(
  67. key != 8 /* backspace */ &&
  68. key != 9 /* tab */ &&
  69. key != 13 /* enter */ &&
  70. key != 35 /* end */ &&
  71. key != 36 /* home */ &&
  72. key != 37 /* left */ &&
  73. key != 39 /* right */ &&
  74. key != 46 /* del */
  75. )
  76. {
  77. allow = false;
  78. }
  79. else
  80. {
  81. // for detecting special keys (listed above)
  82. // IE does not support 'charCode' and ignores them in keypress anyway
  83. if(typeof e.charCode != "undefined")
  84. {
  85. // special keys have 'keyCode' and 'which' the same (e.g. backspace)
  86. if(e.keyCode == e.which && e.which != 0)
  87. {
  88. allow = true;
  89. }
  90. // or keyCode != 0 and 'charCode'/'which' = 0
  91. else if(e.keyCode != 0 && e.charCode == 0 && e.which == 0)
  92. {
  93. allow = true;
  94. }
  95. }
  96. }
  97. // if key pressed is the decimal and it is not already in the field
  98. if(key == decimal.charCodeAt(0))
  99. {
  100. if(!this.containsDecimal)
  101. {
  102. allow = true;
  103. this.containsDecimal = true;
  104. }
  105. else
  106. {
  107. allow = false;
  108. }
  109. }
  110. }
  111. else
  112. {
  113. allow = true;
  114. }
  115. return allow;
  116. }
  117. )
  118. .blur(
  119. function()
  120. {
  121. var val = $(this).val();
  122. if(val != "")
  123. {
  124. var re = new RegExp("^\\d+$|\\d*" + decimal + "\\d+");
  125. if(!re.exec(val))
  126. {
  127. callback.apply(this);
  128. }
  129. }
  130. }
  131. );
  132. return this;
  133. };
  134. })(jQuery);