PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/jquery-validate/demo/multipart/js/jquery.maskedinput-1.0.js

#
JavaScript | 246 lines | 207 code | 6 blank | 33 comment | 7 complexity | da4c4e089d4eb63d427db261c6a0d27c MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. /*
  2. * Copyright (c) 2007 Josh Bush (digitalbush.com)
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use,
  8. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following
  11. * conditions:
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  17. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. /*
  25. * Version: 1.0
  26. * Release: 2007-07-25
  27. */
  28. (function($) {
  29. //Helper Functions for Caret positioning
  30. function getCaretPosition(ctl){
  31. var res = {begin: 0, end: 0 };
  32. if (ctl.setSelectionRange){
  33. res.begin = ctl.selectionStart;
  34. res.end = ctl.selectionEnd;
  35. }else if (document.selection && document.selection.createRange){
  36. var range = document.selection.createRange();
  37. res.begin = 0 - range.duplicate().moveStart('character', -100000);
  38. res.end = res.begin + range.text.length;
  39. }
  40. return res;
  41. };
  42. function setCaretPosition(ctl, pos){
  43. if(ctl.setSelectionRange){
  44. ctl.focus();
  45. ctl.setSelectionRange(pos,pos);
  46. }else if (ctl.createTextRange){
  47. var range = ctl.createTextRange();
  48. range.collapse(true);
  49. range.moveEnd('character', pos);
  50. range.moveStart('character', pos);
  51. range.select();
  52. }
  53. };
  54. //Predefined character definitions
  55. var charMap={
  56. '9':"[0-9]",
  57. 'a':"[A-Za-z]",
  58. '*':"[A-Za-z0-9]"
  59. };
  60. //Helper method to inject character definitions
  61. $.mask={
  62. addPlaceholder : function(c,r){
  63. charMap[c]=r;
  64. }
  65. };
  66. //Main Method
  67. $.fn.mask = function(mask,settings) {
  68. settings = $.extend({
  69. placeholder: "_",
  70. completed: null
  71. }, settings);
  72. //Build Regex for format validation
  73. var reString="^";
  74. for(var i=0;i<mask.length;i++)
  75. reString+=(charMap[mask.charAt(i)] || ("\\"+mask.charAt(i)));
  76. reString+="$";
  77. var re = new RegExp(reString);
  78. return this.each(function(){
  79. var input=$(this);
  80. var buffer=new Array(mask.length);
  81. var locked=new Array(mask.length);
  82. //Build buffer layout from mask
  83. for(var i=0;i<mask.length;i++){
  84. locked[i]=charMap[mask.charAt(i)]==null;
  85. buffer[i]=locked[i]?mask.charAt(i):settings.placeholder;
  86. }
  87. /*Event Bindings*/
  88. input.focus(function(){
  89. checkVal();
  90. writeBuffer();
  91. setCaretPosition(this,0);
  92. });
  93. input.blur(checkVal);
  94. //Paste events for IE and Mozilla thanks to Kristinn Sigmundsson
  95. if ($.browser.msie)
  96. this.onpaste= function(){setTimeout(checkVal,0);};
  97. else if ($.browser.mozilla)
  98. this.addEventListener('input',checkVal,false);
  99. var ignore=false; //Variable for ignoring control keys
  100. input.keydown(function(e){
  101. var pos=getCaretPosition(this);
  102. var k = e.keyCode;
  103. ignore=(k < 16 || (k > 16 && k < 32 ) || (k > 32 && k < 41));
  104. //delete selection before proceeding
  105. if((pos.begin-pos.end)!=0 && (!ignore || k==8 || k==46)){
  106. clearBuffer(pos.begin,pos.end);
  107. }
  108. //backspace and delete get special treatment
  109. if(k==8){//backspace
  110. while(pos.begin-->=0){
  111. if(!locked[pos.begin]){
  112. buffer[pos.begin]=settings.placeholder;
  113. if($.browser.opera){
  114. //Opera won't let you cancel the backspace, so we'll let it backspace over a dummy character.
  115. writeBuffer(pos.begin);
  116. setCaretPosition(this,pos.begin+1);
  117. }else{
  118. writeBuffer();
  119. setCaretPosition(this,pos.begin);
  120. }
  121. return false;
  122. }
  123. }
  124. }else if(k==46){//delete
  125. clearBuffer(pos.begin,pos.begin+1);
  126. writeBuffer();
  127. setCaretPosition(this,pos.begin);
  128. return false;
  129. }else if (k==27){
  130. clearBuffer(0,mask.length);
  131. writeBuffer();
  132. setCaretPosition(this,0);
  133. return false;
  134. }
  135. });
  136. input.keypress(function(e){
  137. if(ignore){
  138. ignore=false;
  139. return;
  140. }
  141. e=e||window.event;
  142. var k=e.charCode||e.keyCode||e.which;
  143. var pos=getCaretPosition(this);
  144. var caretPos=pos.begin;
  145. if(e.ctrlKey || e.altKey){//Ignore
  146. return true;
  147. }else if ((k>=41 && k<=122) ||k==32 || k>186){//typeable characters
  148. while(pos.begin<mask.length){
  149. var reString=charMap[mask.charAt(pos.begin)];
  150. var match;
  151. if(reString){
  152. var reChar=new RegExp(reString);
  153. match=String.fromCharCode(k).match(reChar);
  154. }else{//we're on a mask char, go forward and try again
  155. pos.begin+=1;
  156. pos.end=pos.begin;
  157. caretPos+=1;
  158. continue;
  159. }
  160. if(match)
  161. buffer[pos.begin]=String.fromCharCode(k);
  162. else
  163. return false;//reject char
  164. while(++caretPos<mask.length){//seek forward to next typable position
  165. if(!locked[caretPos])
  166. break;
  167. }
  168. break;
  169. }
  170. }else
  171. return false;
  172. writeBuffer();
  173. if(settings.completed && caretPos>=buffer.length)
  174. settings.completed.call(input);
  175. else
  176. setCaretPosition(this,caretPos);
  177. return false;
  178. });
  179. /*Helper Methods*/
  180. function clearBuffer(start,end){
  181. for(var i=start;i<end;i++){
  182. if(!locked[i])
  183. buffer[i]=settings.placeholder;
  184. }
  185. };
  186. function writeBuffer(pos){
  187. var s="";
  188. for(var i=0;i<mask.length;i++){
  189. s+=buffer[i];
  190. if(i==pos)
  191. s+=settings.placeholder;
  192. }
  193. input.val(s);
  194. return s;
  195. };
  196. function checkVal(){
  197. //try to place charcters where they belong
  198. var test=input.val();
  199. var pos=0;
  200. for(var i=0;i<mask.length;i++){
  201. if(!locked[i]){
  202. while(pos++<test.length){
  203. //Regex Test each char here.
  204. var reChar=new RegExp(charMap[mask.charAt(i)]);
  205. if(test.charAt(pos-1).match(reChar)){
  206. buffer[i]=test.charAt(pos-1);
  207. break;
  208. }
  209. }
  210. }
  211. }
  212. var s=writeBuffer();
  213. if(!s.match(re)){
  214. input.val("");
  215. clearBuffer(0,mask.length);
  216. }
  217. };
  218. });
  219. };
  220. })(jQuery);