README 5.4 KB

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