README 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. jquery.inputmask is a jquery plugin which create an input mask ;-)
  2. The plugin is based on the maskedinput plugin of Josh Bush (http://digitalbush.com/projects/masked-input-plugin), but has finer control over the 'mask-definitions' and is fully compatible with the ui-datepicker
  3. A definition can have a cardinality and have multiple prevalidators.
  4. Example of some new definitions:
  5. 'm': { //month
  6. validator: function(chrs, buffer) {
  7. var dayValue = buffer.join('').substr(0, 3);
  8. return $.inputmask.defaults.aliases['dd/mm/yyyy'].regex.month.test(dayValue + chrs);
  9. },
  10. cardinality: 2,
  11. prevalidator: [{ validator: "[01]", cardinality: 1}]
  12. },
  13. 'y': { //year
  14. validator: function(chrs, buffer) {
  15. if ($.inputmask.defaults.aliases['dd/mm/yyyy'].regex.year.test(chrs)) {
  16. var dayMonthValue = buffer.join('').substr(0, 6);
  17. if (dayMonthValue != "29/02/")
  18. return true;
  19. else {
  20. var year = parseInt(chrs); //detect leap year
  21. if (year % 4 == 0)
  22. if (year % 100 == 0)
  23. if (year % 400 == 0)
  24. return true;
  25. else return false;
  26. else return true;
  27. else return false;
  28. }
  29. } else return false;
  30. },
  31. cardinality: 4,
  32. prevalidator: [
  33. { validator: "[12]", cardinality: 1 },
  34. { validator: "(19|20)", cardinality: 2 },
  35. { validator: "(19|20)\\d", cardinality: 3 }
  36. ]
  37. }
  38. },
  39. insertMode: false
  40. These allow for a finer date validation then 99/99/9999 which also allows 33/33/3333 for example.
  41. In the jquery.inputmask.extentions.js you find a full date input validation which takes days, months & leap years into account.
  42. Also extra features like mask-repetitions (greedy and non-gready) and many other additions are included. In the examples you will find more about them.
  43. Usage:
  44. Include the js-files
  45. <script src="jquery.js" type="text/javascript"></script>
  46. <script src="jquery.inputmask.js" type="text/javascript"></script>
  47. <script src="jquery.inputmask.extentions.js" type="text/javascript"></script>
  48. Define your masks:
  49. $(document).ready(function(){
  50. $("#date").inputmask("d/m/y"); //direct mask
  51. $("#phone").inputmask("mask", {"mask": "(999) 999-9999"}); //specifying fn & options
  52. $("#tin").inputmask({"mask": "99-9999999"}); //specifying options only
  53. });
  54. Extra options:
  55. change the placeholder
  56. $(document).ready(function(){
  57. $("#date").inputmask("d/m/y",{ "placeholder": "*" });
  58. });
  59. or a multi-char placeholder
  60. $(document).ready(function(){
  61. $("#date").inputmask("d/m/y",{ "placeholder": "dd/mm/yyyy" });
  62. });
  63. execute a function when the mask is completed, incomplete or cleared
  64. $(document).ready(function(){
  65. $("#date").inputmask("d/m/y",{ "oncomplete": function(){ alert('inputmask complete'); } });
  66. $("#date").inputmask("d/m/y",{ "onincomplete": function(){ alert('inputmask incomplete'); } });
  67. $("#date").inputmask("d/m/y",{ "oncleared": function(){ alert('inputmask cleared'); } });
  68. });
  69. mask repeat function
  70. $(document).ready(function(){
  71. $("#number").inputmask({ "mask": "9", "repeat": 10 }); // ~ mask "9999999999"
  72. });
  73. mask non-greedy repeat function
  74. $(document).ready(function(){
  75. $("#number").inputmask({ "mask": "9", "repeat": 10, "greedy": false }); // ~ mask "9" or mask "99" or ... mask "9999999999"
  76. });
  77. get the unmaskedvalue
  78. $(document).ready(function(){
  79. $("#number").inputmask('unmaskedvalue');
  80. });
  81. set a value and apply mask
  82. $(document).ready(function(){
  83. $("#number").inputmask('setvalue', 12345);
  84. });
  85. when the option patch_eval is set to true the same can be done with the traditionnal jquery.val function
  86. $(document).ready(function(){
  87. $("#number").val(12345);
  88. });
  89. with the autoUnmaskoption you can change the return of $.fn.val to unmaskedvalue or the maskedvalue
  90. $(document).ready(function(){
  91. $('#<%= tbDate.ClientID%>').inputmask({ "mask": "d/m/y", 'autoUnmask' : true}); // value: 23/03/1973
  92. alert($('#<%= tbDate.ClientID%>').val()); // shows 23031973 (autoUnmask: true)
  93. });
  94. add custom definitions
  95. $.extend($.inputmask.defaults.definitions, {
  96. 'f': {
  97. "validator": "[0-9\(\)\.\+/ ]",
  98. "cardinality": 1,
  99. 'prevalidator': null
  100. }
  101. });
  102. set defaults
  103. $.extend($.inputmask.defaults, {
  104. 'autounmask': true
  105. });
  106. numeric input direction
  107. $(document).ready(function(){
  108. $('#test').inputmask('€ 999.999.999,99', { numericInput: true }); // 123456 => € ___.__1.234,56
  109. });
  110. remove the inputmask
  111. $(document).ready(function(){
  112. $('#test').inputmask('remove');
  113. });
  114. escape special mask chars
  115. $(document).ready(function(){
  116. $("#months").inputmask("m \\months");
  117. });
  118. remove incomplete input on blur - clearIncomplete
  119. $(document).ready(function(){
  120. $("#ssn").inputmask("999-99-9999",{placeholder:" ", clearIncomplete: true });
  121. });
  122. oncleared option
  123. $(document).ready(function(){
  124. $("#ssn").inputmask("999-99-9999",{placeholder:" ", oncleared: function(){ alert('Set focus somewhere else ;-)');} });
  125. });
  126. aliases option
  127. First you have to create an alias definition (more examples can be found in jquery.inputmask.extentions.js)
  128. $.extend($.inputmask.defaults.aliases, {
  129. 'date': {
  130. mask: "d/m/y"
  131. },
  132. 'dd/mm/yyyy': {
  133. alias: "date"
  134. }
  135. });
  136. use;
  137. $(document).ready(function(){
  138. $("#date").inputmask("date"); => equals to $("#date").inputmask("d/m/y");
  139. });
  140. or use the dd/mm/yyyy alias of the date alias:
  141. $(document).ready(function(){
  142. $("#date").inputmask("dd/mm/yyyy"); => equals to $("#date").inputmask("d/m/y");
  143. });
  144. auto upper/lower- casing inputmask
  145. see jquery.inputmask.extentions.js for an example how to define "auto"-casing in a definition (definition A)
  146. casing can be null, "upper" or "lower"
  147. $(document).ready(function(){
  148. $("#test").inputmask("999-AAA"); => 123abc ===> 123-ABC
  149. });
  150. getemptymask command
  151. return the default (empty) mask value
  152. $(document).ready(function(){
  153. $("#test").inputmask("999-AAA");
  154. alert($("#test").inputmask("getemptymask")); => "___-___"
  155. });
  156. RTL input
  157. Just add the dir="rtl" attribute to the input element
  158. <input id="test" dir="rtl" />
  159. -----------------------------------------