README 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. execute a function when the mask is completed
  43. $(document).ready(function(){
  44. $("#date").inputmask("d/m/y",{ "oncomplete": function(){ alert('inputmask complete'); } });
  45. });
  46. mask repeat function
  47. $(document).ready(function(){
  48. $("#number").inputmask({ "mask": "9", "repeat": 10 }); // ~ mask "9999999999"
  49. });
  50. mask non-greedy repeat function
  51. $(document).ready(function(){
  52. $("#number").inputmask({ "mask": "9", "repeat": 10, "greedy": false }); // ~ mask "9" or mask "99" or ... mask "9999999999"
  53. });
  54. get the unmaskedvalue
  55. $(document).ready(function(){
  56. $("#number").inputmask('unmaskedvalue');
  57. });
  58. set a value and apply mask
  59. $(document).ready(function(){
  60. $("#number").inputmask('setvalue', 12345);
  61. });
  62. when the option patch_eval is set to true the same can be done with the traditionnal jquery.val function
  63. $(document).ready(function(){
  64. $("#number").val(12345);
  65. });
  66. with the autoUnmaskoption you can change the return of $.fn.val to unmaskedvalue or the maskedvalue
  67. $(document).ready(function(){
  68. $('#<%= tbDate.ClientID%>').inputmask({ "mask": "d/m/y", 'autoUnmask' : true}); // value: 23/03/1973
  69. alert($('#<%= tbDate.ClientID%>').val()); // shows 23031973 (autoUnmask: true)
  70. });
  71. add custom definitions
  72. $.extend($.inputmask.defaults.definitions, {
  73. 'f': {
  74. "validator": "[0-9\(\)\.\+/ ]",
  75. "cardinality": 1,
  76. 'prevalidator': null
  77. }
  78. });
  79. set defaults
  80. $.extend($.inputmask.defaults, {
  81. 'autounmask': true
  82. });
  83. numeric input direction
  84. $(document).ready(function(){
  85. $('#test').inputmask('€ 999.999.999,99', { numericInput: true }); // 123456 => € ___.__1.234,56
  86. });
  87. remove the inputmask
  88. $(document).ready(function(){
  89. $('#test').inputmask('remove');
  90. });
  91. escape special mask chars
  92. $(document).ready(function(){
  93. $("#months").inputmask("m \\months");
  94. });
  95. remove incomplete input on blur - clearIncomplete
  96. $(document).ready(function(){
  97. $("#ssn").inputmask("999-99-9999",{placeholder:" ", clearIncomplete: true });
  98. });
  99. oncleared option
  100. $(document).ready(function(){
  101. $("#ssn").inputmask("999-99-9999",{placeholder:" ", oncleared: function(){ alert('Set focus somewhere else ;-)');} });
  102. });
  103. aliases option
  104. First you have to create an alias definition (more examples can be found in jquery.inputmask.extentions.js)
  105. $.extend($.inputmask.defaults.aliases, {
  106. 'date': {
  107. mask: "d/m/y"
  108. },
  109. 'dd/mm/yyyy': {
  110. alias: "date"
  111. }
  112. });
  113. use;
  114. $(document).ready(function(){
  115. $("#date").inputmask("date"); => equals to $("#date").inputmask("d/m/y");
  116. });
  117. or use the dd/mm/yyyy alias of the data alias:
  118. $(document).ready(function(){
  119. $("#date").inputmask("dd/mm/yyyy"); => equals to $("#date").inputmask("d/m/y");
  120. });