README 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. Also extra features like mask-repetitions (greedy and non-gready) are included. In the examples you will find more about them.
  26. Usage:
  27. Include the js-files
  28. <script src="jquery.js" type="text/javascript"></script>
  29. <script src="jquery.inputmask.js" type="text/javascript"></script>
  30. Define your masks:
  31. $(document).ready(function(){
  32. $("#date").inputmask("d/m/y"); //direct mask
  33. $("#phone").inputmask("mask", {"mask": "(999) 999-9999"}); //specifying fn & options
  34. $("#tin").inputmask({"mask": "99-9999999"}); //specifying options only
  35. });
  36. Extra options:
  37. change the placeholder
  38. $(document).ready(function(){
  39. $("#date").inputmask("d/m/y",{ "placeholder": "*" });
  40. });
  41. execute a function when the mask is completed
  42. $(document).ready(function(){
  43. $("#date").inputmask("d/m/y",{ "oncomplete": function(){ alert('inputmask complete'); } });
  44. });
  45. mask repeat function
  46. $(document).ready(function(){
  47. $("#number").inputmask({ "mask": "9", "repeat": 10 }); // ~ mask "9999999999"
  48. });
  49. mask non-greedy repeat function
  50. $(document).ready(function(){
  51. $("#number").inputmask({ "mask": "9", "repeat": 10, "greedy": false }); // ~ mask "9" or mask "99" or ... mask "9999999999"
  52. });
  53. get the unmaskedvalue
  54. $(document).ready(function(){
  55. $("#number").inputmask('unmaskedvalue');
  56. });
  57. set a value and apply mask
  58. $(document).ready(function(){
  59. $("#number").inputmask('setvalue', 12345);
  60. });
  61. when the option patch_eval is set to true the same can be done with the traditionnal jquery.val function
  62. $(document).ready(function(){
  63. $("#number").val(12345);
  64. });
  65. with the autounmaskoption you can change the return of $.fn.val to unmaskedvalue or the maskedvalue
  66. $(document).ready(function(){
  67. $('#<%= tbDate.ClientID%>').inputmask({ "mask": "d/m/y", 'autounmask' : true}); // value: 23/03/1973
  68. alert($('#<%= tbDate.ClientID%>').val()); // shows 23031973 (autounmask: true)
  69. });
  70. add custom definitions
  71. $.extend($.inputmask.defaults.definitions, {
  72. 'f': {
  73. "validator": "[0-9\(\)\.\+/ ]",
  74. "cardinality": 1,
  75. 'prevalidator': null
  76. }
  77. });
  78. set defaults
  79. $.extend($.inputmask.defaults, {
  80. 'autounmask': true
  81. });
  82. optional masks
  83. $(document).ready(function(){
  84. $('#test1').inputmask('9999[aaaa]9999'); // 1234abcd1234 or 12341234
  85. $('#test2').inputmask('[aaaa]9999'); // abcd1234 or 1234
  86. $('#test3').inputmask('9999[aaaa]'); // 1234 or 1234abcd
  87. });