Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

jquery.autotab.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**
  2. * Autotab - jQuery plugin 1.1b
  3. * http://www.lousyllama.com/sandbox/jquery-autotab
  4. *
  5. * Copyright (c) 2008 Matthew Miller
  6. *
  7. * Dual licensed under the MIT and GPL licenses:
  8. * http://www.opensource.org/licenses/mit-license.php
  9. * http://www.gnu.org/licenses/gpl.html
  10. *
  11. * Revised: 2008-09-10 16:55:08
  12. */
  13. (function($) {
  14. // Look for an element based on ID or name
  15. var check_element = function(name) {
  16. var obj = null;
  17. var check_id = $('#' + name);
  18. var check_name = $('input[name=' + name + ']');
  19. if(check_id.length)
  20. obj = check_id;
  21. else if(check_name != undefined)
  22. obj = check_name;
  23. return obj;
  24. };
  25. /**
  26. * autotab_magic automatically establishes autotabbing with the
  27. * next and previous elements as provided by :input.
  28. *
  29. * autotab_magic should called after applying filters, if used.
  30. * If any filters are applied after calling autotab_magic, then
  31. * Autotab may not protect against brute force typing.
  32. *
  33. * @name autotab_magic
  34. * @param focus Applies focus on the specified element
  35. * @example $(':input').autotab_magic();
  36. */
  37. $.fn.autotab_magic = function(focus) {
  38. for(var i = 0; i < this.length; i++)
  39. {
  40. var n = i + 1;
  41. var p = i - 1;
  42. if(i > 0 && n < this.length)
  43. $(this[i]).autotab({ target: $(this[n]), previous: $(this[p]) });
  44. else if(i > 0)
  45. $(this[i]).autotab({ previous: $(this[p]) });
  46. else
  47. $(this[i]).autotab({ target: $(this[n]) });
  48. // Set the focus on the specified element
  49. if(focus != null && (isNaN(focus) && focus == $(this[i]).attr('id')) || (!isNaN(focus) && focus == i))
  50. $(this[i]).focus();
  51. }
  52. return this;
  53. };
  54. /**
  55. * This will take any of the text that is typed and
  56. * format it according to the options specified.
  57. *
  58. * Option values:
  59. * format text|number|alphanumeric|all|custom
  60. * - Text Allows all characters except numbers
  61. * - Number Allows only numbers
  62. * - Alphanumeric Allows only letters and numbers
  63. * - All Allows any and all characters
  64. * - Custom Allows developer to provide their own filter
  65. *
  66. * uppercase true|false
  67. * - Converts a string to UPPERCASE
  68. *
  69. * lowercase true|false
  70. * - Converts a string to lowecase
  71. *
  72. * nospace true|false
  73. * - Remove spaces in the user input
  74. *
  75. * pattern null|(regular expression)
  76. * - Custom regular expression for the filter
  77. *
  78. * @name autotab_filter
  79. * @param options Can be a string, function or a list of options. If a string or
  80. * function is passed, it will be assumed to be a format option.
  81. * @example $('#number1, #number2, #number3').autotab_filter('number');
  82. * @example $('#product_key').autotab_filter({ format: 'alphanumeric', nospace: true });
  83. * @example $('#unique_id').autotab_filter({ format: 'custom', pattern: '[^0-9\.]' });
  84. */
  85. $.fn.autotab_filter = function(options) {
  86. var defaults = {
  87. format: 'all',
  88. uppercase: false,
  89. lowercase: false,
  90. nospace: false,
  91. pattern: null
  92. };
  93. if(typeof options == 'string' || typeof options == 'function')
  94. defaults.format = options;
  95. else
  96. $.extend(defaults, options);
  97. for(var i = 0; i < this.length; i++)
  98. {
  99. $(this[i]).bind('keyup', function(e) {
  100. var val = this.value;
  101. switch(defaults.format)
  102. {
  103. case 'text':
  104. var pattern = new RegExp('[0-9]+', 'g');
  105. val = val.replace(pattern, '');
  106. break;
  107. case 'alpha':
  108. var pattern = new RegExp('[^a-zA-Z]+', 'g');
  109. val = val.replace(pattern, '');
  110. break;
  111. case 'number':
  112. case 'numeric':
  113. var pattern = new RegExp('[^0-9]+', 'g');
  114. val = val.replace(pattern, '');
  115. break;
  116. case 'alphanumeric':
  117. var pattern = new RegExp('[^0-9a-zA-Z]+', 'g');
  118. val = val.replace(pattern, '');
  119. break;
  120. case 'custom':
  121. var pattern = new RegExp(defaults.pattern, 'g');
  122. val = val.replace(pattern, '');
  123. break;
  124. case 'all':
  125. default:
  126. if(typeof defaults.format == 'function')
  127. var val = defaults.format(val);
  128. break;
  129. }
  130. if(defaults.nospace)
  131. {
  132. var pattern = new RegExp('[ ]+', 'g');
  133. val = val.replace(pattern, '');
  134. }
  135. if(defaults.uppercase)
  136. val = val.toUpperCase();
  137. if(defaults.lowercase)
  138. val = val.toLowerCase();
  139. if(val != this.value)
  140. this.value = val;
  141. });
  142. }
  143. };
  144. /**
  145. * Provides the autotabbing mechanism for the supplied element and passes
  146. * any formatting options to autotab_filter.
  147. *
  148. * Refer to autotab_filter's description for a detailed explanation of
  149. * the options available.
  150. *
  151. * @name autotab
  152. * @param options
  153. * @example $('#phone').autotab({ format: 'number' });
  154. * @example $('#username').autotab({ format: 'alphanumeric', target: 'password' });
  155. * @example $('#password').autotab({ previous: 'username', target: 'confirm' });
  156. */
  157. $.fn.autotab = function(options) {
  158. var defaults = {
  159. format: 'all',
  160. maxlength: 2147483647,
  161. uppercase: false,
  162. lowercase: false,
  163. nospace: false,
  164. target: null,
  165. previous: null,
  166. pattern: null
  167. };
  168. $.extend(defaults, options);
  169. // Sets targets to element based on the name or ID
  170. // passed if they are not currently objects
  171. if(typeof defaults.target == 'string')
  172. defaults.target = check_element(defaults.target);
  173. if(typeof defaults.previous == 'string')
  174. defaults.previous = check_element(defaults.previous);
  175. var maxlength = $(this).attr('maxlength');
  176. // defaults.maxlength has not changed and maxlength was specified
  177. if(defaults.maxlength == 2147483647 && maxlength != 2147483647)
  178. defaults.maxlength = maxlength;
  179. // defaults.maxlength overrides maxlength
  180. else if(defaults.maxlength > 0)
  181. $(this).attr('maxlength', defaults.maxlength)
  182. // defaults.maxlength and maxlength have not been specified
  183. // A target cannot be used since there is no defined maxlength
  184. else
  185. defaults.target = null;
  186. if(defaults.format != 'all')
  187. $(this).autotab_filter(defaults);
  188. // Go to the previous element when backspace
  189. // is pressed in an empty input field
  190. return $(this).bind('keydown', function(e) {
  191. if(e.which == 8 && this.value.length == 0 && defaults.previous)
  192. defaults.previous.focus().val(defaults.previous.val());
  193. }).bind('keyup', function(e) {
  194. /**
  195. * Do not auto tab when the following keys are pressed
  196. * 8: Backspace
  197. * 9: Tab
  198. * 16: Shift
  199. * 17: Ctrl
  200. * 18: Alt
  201. * 19: Pause Break
  202. * 20: Caps Lock
  203. * 27: Esc
  204. * 33: Page Up
  205. * 34: Page Down
  206. * 35: End
  207. * 36: Home
  208. * 37: Left Arrow
  209. * 38: Up Arrow
  210. * 39: Right Arrow
  211. * 40: Down Arrow
  212. * 45: Insert
  213. * 46: Delete
  214. * 144: Num Lock
  215. * 145: Scroll Lock
  216. */
  217. var keys = [8, 9, 16, 17, 18, 19, 20, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 144, 145];
  218. if(e.which != 8)
  219. {
  220. var val = $(this).val();
  221. if($.inArray(e.which, keys) == -1 && val.length == defaults.maxlength && defaults.target)
  222. defaults.target.focus();
  223. }
  224. });
  225. };
  226. })(jQuery);