InputMask jQuery插件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了InputMask jQuery插件相关的知识,希望对你有一定的参考价值。

Based on my Annotations plugin this plugin offers the ability to use Mask annotations to apply input masks over input elements on a page. Very much BETA. See comments for use.

  1. /*---------------------------------------------------------------------------------
  2.  *
  3.  * InputMask jQuery Plugin
  4.  *
  5.  *---------------------------------------------------------------------------------
  6.  *
  7.  * Taking alot of inspiration and code from
  8.  * http://digitalbush.com/projects/masked-input-plugin this is a masked input
  9.  * solution that should handle most cases. It uses annotations to determine the
  10.  * actual mask. Mask characters include,
  11.  *
  12.  * % - Any digit or numeric sign
  13.  * # - Any digit
  14.  * @ - Any letter
  15.  * * - Any letter or digit
  16.  * ~ - Any sign (+/-)
  17.  * ? - Currencies ($,£,€,Â¥)
  18.  *
  19.  * @author James Hughes
  20.  *
  21.  * -------------------------------------------------------------------------------
  22.  * 29/10/2008 - Initial Version
  23.  * -------------------------------------------------------------------------------
  24.  *
  25.  *///------------------------------------------------------------------------------
  26. (function($){
  27.  
  28. /*-----------------------------------------------------------------------------
  29.   *
  30.   * availableMasks
  31.   *
  32.   *-----------------------------------------------------------------------------
  33.   *
  34.   * Available Character Masks. This can be extended or modified via the
  35.   * $.mask.availableMasks config.
  36.   *
  37.   *///--------------------------------------------------------------------------
  38. var availableMasks = {
  39. '%' : '[-+0-9]', // Any digit or numeric sign
  40. '#' : '[0-9]', // Any digit
  41. '@' : '[A-Za-z]', // Any letter
  42. '*' : '[A-Za-z0-9]', // Any letter or digit
  43. '~' : '[+-]', // Any sign (+/-)
  44. '?' : '[$£€¥]' // Typical World Currencies
  45. }
  46.  
  47. /*-----------------------------------------------------------------------------
  48.   *
  49.   * $.applyMasks
  50.   *
  51.   *-----------------------------------------------------------------------------
  52.   *
  53.   * Deteremines, based @Mask on annotations, all elements below either the
  54.   * specified root or the document element that should have masks applied
  55.   *
  56.   * @plugin
  57.   *
  58.   * @param opts - options
  59.   *
  60.   *///--------------------------------------------------------------------------
  61. $.applyMasks = function(root, opts){
  62. $.annotated("@Mask", root || document).mask(opts);
  63. }
  64.  
  65. /*-----------------------------------------------------------------------------
  66.   *
  67.   * $.fn.mask
  68.   *
  69.   *-----------------------------------------------------------------------------
  70.   *
  71.   * Applies the annotated masks to the passed in elements. Applicable options
  72.   *
  73.   * invalidHandler custom event fired when field blurs invalid
  74.   * placeholder placeholder for mask characters. defaults to _
  75.   * alwaysShowMask determine if we always show the input mask
  76.   * permitIncomplete determine if we allow the field to be partially
  77.   * filled on blur.
  78.   * selectOnFocus : true
  79.   *
  80.   *///--------------------------------------------------------------------------
  81. $.fn.mask = function(opts){
  82.  
  83. /*-------------------------------------------------------------------------
  84.   *
  85.   * Apply Mask
  86.   *
  87.   *-------------------------------------------------------------------------
  88.   *
  89.   * This section discovers the required mask on a per field basis and
  90.   * applies the behaviour to the field
  91.   *
  92.   *///----------------------------------------------------------------------
  93. return this.each(function(){
  94.  
  95. /*---------------------------------------------------------------------
  96.   *
  97.   * No Mask Annotation Failover
  98.   *
  99.   *---------------------------------------------------------------------
  100.   *
  101.   * Most of this API is open to the public therefore open to the
  102.   * irresponsible, ignorant, clueless and just plain stupid. We need
  103.   * to cater for as much worst case edge cases as we can without
  104.   * making the good people suffer. Exit if no mask defined on the
  105.   * element.
  106.   *
  107.   *///------------------------------------------------------------------
  108. if(!$(this).annotations("@Mask")[0]){ return undefined };
  109.  
  110. /*---------------------------------------------------------------------
  111.   *
  112.   * Apply Options
  113.   *
  114.   *---------------------------------------------------------------------
  115.   *
  116.   * Merge the default and custom options resulting in a specific
  117.   * options map for this function call.
  118.   *
  119.   *///------------------------------------------------------------------
  120. var o = $.extend({}, defaultOptions, opts);
  121.  
  122. /*---------------------------------------------------------------------
  123.   *
  124.   * Assign Buffers
  125.   *
  126.   *---------------------------------------------------------------------
  127.   *
  128.   * Iterate over the jQuery collection of fields passed in and add
  129.   * the intial buffer data to each.
  130.   *
  131.   *///------------------------------------------------------------------
  132. $(this).each(function(){
  133. $(this).data("mask-buffer", new MaskBuffer(
  134. $(this).annotations("@Mask")[0].data, o.placeholder
  135. ));
  136. });
  137.  
  138. /*---------------------------------------------------------------------
  139.   *
  140.   * Handle Blur
  141.   *
  142.   *---------------------------------------------------------------------
  143.   *
  144.   * When a masked field blurs we need to handle overall validation and
  145.   * based on the confguration options hide and show the mask. If the
  146.   * field is invalid the event will fire the custom handler.
  147.   *
  148.   *///------------------------------------------------------------------
  149. function handleBlur(){
  150.  
  151. var buffer = $(this).data("mask-buffer");
  152.  
  153. if(!o.permitIncomplete){
  154.  
  155. var v = $(this).val();
  156. if(((!v || v === "") || !buffer.test())){
  157.  
  158. buffer.reset();
  159. $(this).val( o.alwaysShowMask ? buffer.get() : "" );
  160.  
  161. o.invalidHandler(this, v);
  162. }
  163. }
  164. }
  165.  
  166. /*---------------------------------------------------------------------
  167.   *
  168.   * Handle Key Press
  169.   *
  170.   *---------------------------------------------------------------------
  171.   *
  172.   * In the keypress event we are responsible for handling the standard
  173.   * keys and managing the buffer
  174.   *
  175.   *///------------------------------------------------------------------
  176. function handleKeyPress(e){
  177.  
  178. var code = ($.browser.msie) ? e.which : e.charCode;
  179.  
  180. if(code != 0 && !(e.ctrlKey || e.altKey)){
  181.  
  182. var buffer = $(this).data("mask-buffer");
  183.  
  184. var carat = $.carat.get(this);
  185. var ncp = buffer.nextMaskPosition(carat.start-1);
  186.  
  187. if(ncp < buffer.size()){
  188.  
  189. var characterTest = new RegExp(availableMasks[buffer.getMaskValue(ncp)]);
  190. var value = String.fromCharCode(code);
  191.  
  192. if(characterTest.test(value)){
  193. buffer.set(ncp, value);
  194. $(this).val(buffer.get());
  195. $.carat.set(this, new Carat(buffer.nextMaskPosition(ncp)));
  196. }
  197. }
  198.  
  199. /* handle ourselves */
  200. return false;
  201. }
  202. }
  203.  
  204. /*---------------------------------------------------------------------
  205.   *
  206.   * Handle Key Down
  207.   *
  208.   *---------------------------------------------------------------------
  209.   *
  210.   * In the key down we are responsible for handling special characters
  211.   * such as delete, backspace and escape. As well as clearing any
  212.   * selections
  213.   *
  214.   *///------------------------------------------------------------------
  215. function handleKeyDown(e){
  216.  
  217. /*-----------------------------------------------------------------
  218.   *
  219.   * Key Code Constants
  220.   *
  221.   *-----------------------------------------------------------------
  222.   *
  223.   * Constant representing the useful keycodes
  224.   *
  225.   *///--------------------------------------------------------------
  226. var BACKSPACE = 8;
  227. var DELETE = 46;
  228. var ESCAPE = 27;
  229.  
  230. var carat = $.carat.get(this);
  231. var code = e.keyCode;
  232. var buffer = $(this).data("mask-buffer");
  233.  
  234. if(carat.isSelection() && (code == BACKSPACE || code == DELETE)){
  235. buffer.reset(carat.start, carat.end);
  236. }
  237.  
  238. switch(code){
  239. case BACKSPACE:
  240.  
  241. while(carat.start-- >= 0){
  242. if(availableMasks[buffer.getMaskValue(carat.start)]){
  243.  
  244. buffer.reset(carat.start);
  245.  
  246. if($.browser.opera){
  247. /* Opera can't cancel backspace, prevent deletion */
  248. $(this).val(buffer.get().substring(0, carat.start) + " " + buffer.get().substring(carat.start));
  249. $.carat.set(this, new Carat(carat.start++));
  250. }else{
  251. $(this).val(buffer.get());
  252. $.carat.set(this, new Carat(Math.max(buffer.getStartingMaskPosition(), carat.start)));
  253. }
  254.  
  255. return false;
  256. }
  257. }
  258. break;
  259.  
  260. case DELETE:
  261.  
  262. buffer.reset(carat.start);
  263. $(this).val(buffer.get());
  264. $.carat.set(this, new Carat(Math.max(buffer.getStartingMaskPosition(), carat.start)));
  265.  
  266. return false;
  267.  
  268. case ESCAPE:
  269.  
  270. buffer.reset();
  271. $(this).val(buffer.get());
  272. $.carat.set(this, new Carat(buffer.getStartingMaskPosition()));
  273.  
  274. return false;
  275. }
  276. }
  277.  
  278. /*---------------------------------------------------------------------
  279.   *
  280.   * Handle Focus
  281.   *
  282.   *---------------------------------------------------------------------
  283.   *
  284.   * On focus we want to set the value to the current buffer state and
  285.   * determine where we set the carat
  286.   *
  287.   *///------------------------------------------------------------------
  288. function handleFocus(){
  289. $(this).val($(this).data("mask-buffer").get());
  290.  
  291. if(o.selectOnFocus){
  292. $.carat.set(this, new Carat(0, $(this).val().length));
  293. }else{
  294. $.carat.set(this, new Carat(Math.max(start, this.value.indexOf(o.placeholder))));
  295. }
  296. }
  297.  
  298. /*---------------------------------------------------------------------
  299.   *
  300.   * Handle Paste
  301.   *
  302.   *---------------------------------------------------------------------
  303.   *
  304.   * Custom event used to handle onpaste events. When we paste data
  305.   * into a masked field we loop over the buffer and only apply the
  306.   * valid parts of the paste.
  307.   *
  308.   * This method is not ideal but it does the job for now.
  309.   *
  310.   *///------------------------------------------------------------------
  311. function handlePaste(){
  312.  
  313. var currentValue = $(this).val().split('');
  314. var buffer = $(this).data("mask-buffer");
  315.  
  316. for(var i = 0; i < buffer.size(); i++){
  317. if(availableMasks[buffer.getMaskValue(i)]){
  318. var re = new RegExp(availableMasks[buffer.getMaskValue(i)]);
  319. if(re.test(currentValue[i])){
  320. buffer.set(i, currentValue[i])
  321. }else{
  322. buffer.reset(i);
  323. }
  324. }
  325. }
  326.  
  327. $(this).val(buffer.get());
  328.  
  329. $.carat.set(this, new Carat((this.value.indexOf(o.placeholder) == -1)?buffer.size():this.value.indexOf(o.placeholder)));
  330.  
  331. handleBlur.call(this);
  332. }
  333.  
  334. /*---------------------------------------------------------------------
  335.   *
  336.   * Bind Events
  337.   *
  338.   *---------------------------------------------------------------------
  339.   *
  340.   * Bind the mask events to the current field. Include the custom
  341.   * paste event.
  342.   *
  343.   *///------------------------------------------------------------------
  344. $(this).bind('blur', handleBlur);
  345. $(this).bind('keypress', handleKeyPress);
  346. $(this).bind('focus', handleFocus);
  347. $(this).bind('keydown', handleKeyDown);
  348.  
  349. if ($.browser.msie){
  350. this.onpaste = function(){ setTimeout(handlePaste,0); };
  351. }else if ($.browser.mozilla){
  352. this.addEventListener('input', handlePaste, false);
  353. }
  354.  
  355. /*---------------------------------------------------------------------
  356.   *
  357.   * Unmask Event
  358.   *
  359.   *---------------------------------------------------------------------
  360.   *
  361.   * Add the unmask event to be fired only once. This will remove all
  362.   * the mask associated data and event listeners from the field
  363.   *
  364.   *///------------------------------------------------------------------
  365. $(this).one('unmask', function(){
  366.  
  367. /* Unbond Events */
  368. $(this).unbind('blur', handleBlur);
  369. $(this).unbind('keypress', handleKeyPress);
  370. $(this).unbind('focus', handleFocus);
  371. $(this).unbind('keydown', handleKeyDown);
  372.  
  373. /* Remove Buffer Data */
  374. $(this).removeData("mask-buffer");
  375.  
  376. /* Remove Custom Paste Event */
  377. if ($.browser.msie){
  378. this.onpaste = null;
  379. }else if ($.browser.mozilla){
  380. this.removeEventListener('input',handlePaste,false);
  381. }
  382.  
  383. });
  384.  
  385. /*---------------------------------------------------------------------
  386.   *
  387.   * Initialize Field
  388.   *
  389.   *---------------------------------------------------------------------
  390.   *
  391.   * Call the handlePaste event to intialize the field. This will
  392.   * handle any existing text that is there and set the inital value of
  393.   * the field.
  394.   *
  395.   *///------------------------------------------------------------------
  396. handlePaste.call(this);
  397. });
  398. }
  399.  
  400. /*-----------------------------------------------------------------------------
  401.   *
  402.   * $.fn.unmask
  403.   *
  404.   *-----------------------------------------------------------------------------
  405.   *
  406.   * Remove any masks from the passed in fields.
  407.   *
  408.   *///--------------------------------------------------------------------------
  409. $.fn.unmask = function(){
  410. return this.trigger('unmask');
  411. }
  412.  
  413. /*-----------------------------------------------------------------------------
  414.   *
  415.   * Default Options
  416.   *
  417.   *-----------------------------------------------------------------------------
  418.   *
  419.   * This map represents the global default options that should be used
  420.   * when applying constraints. They can be overridden via custom maps
  421.   * passed into the functions.
  422.   *
  423.   *///--------------------------------------------------------------------------
  424. var defaultOptions = {
  425. invalidHandler : function(el){},
  426. placeholder : "_",
  427. alwaysShowMask : true,
  428. permitIncomplete : false,
  429. selectOnFocus : true
  430. };
  431.  
  432.  
  433. /*-----------------------------------------------------------------------------
  434.   *
  435.   * Carat Object
  436.   *
  437.   *-----------------------------------------------------------------------------
  438.   *
  439.   * The Carat object encapsulates carat functionality such as setting and
  440.   * getting selections and postions.
  441.   *
  442.   * @constructor
  443.   *
  444.   *///--------------------------------------------------------------------------
  445. var Carat = function(s,e){
  446. this.start = s || 0;
  447. this.end = e || s || 0;
  448. }
  449.  
  450. Carat.prototype = {
  451.  
  452. /*-------------------------------------------------------------------------
  453.   *
  454.   * isSelection
  455.   *
  456.   *-------------------------------------------------------------------------
  457.   *
  458.   * Determines if the current carat position is a selection or not
  459.   *
  460.   *///----------------------------------------------------------------------
  461. isSelection:function(){
  462. return this.start < this.end;
  463. },
  464. start : this.start,
  465. end : this.end
  466. }
  467.  
  468. /*-----------------------------------------------------------------------------
  469.   *
  470.   * getCaratPosition
  471.   *
  472.   *-----------------------------------------------------------------------------
  473.   *
  474.   * Based on the passed in input field this function will return the current
  475.   * carat position as an object with a start and end property. This allows
  476.   * for both single carat positions and whole selection ranges
  477.   *
  478.   * @param el element to extract carat position from
  479.   *
  480.   *///--------------------------------------------------------------------------
  481. Carat.getCaratPosition = function(el){
  482. if (el.setSelectionRange){
  483. return new Carat(el.selectionStart, el.selectionEnd);
  484. }else if (document.selection && document.selection.createRange){
  485. var range = document.selection.createRange();
  486. var start = 0 - range.duplicate().moveStart('character', -100000);
  487.  
  488. return new Carat(start, start + range.text.length);
  489. }
  490. }
  491.  
  492. /*-----------------------------------------------------------------------------
  493.   *
  494.   * Set Carat Position
  495.   *
  496.   *-----------------------------------------------------------------------------
  497.   *
  498.   * Sets the postion of the carat on the passed in element. Can be a single
  499.   * postion of a selection.
  500.   *
  501.   * @param el element to set carat position omn
  502.   * @param from start position of carat
  503.   * @param to end position of carat (optional)
  504.   *
  505.   *///--------------------------------------------------------------------------
  506. Carat.setCaratPosition = function(el, c){
  507. if(el.setSelectionRange){
  508. el.focus();
  509. el.setSelectionRange(c.start,c.end);
  510. }else if (el.createTextRange){
  511. var range = el.createTextRange();
  512. range.collapse(true);
  513. range.moveEnd('character', c.end);
  514. range.moveStart('character', c.start);
  515. range.select();
  516. }
  517. }
  518.  
  519.  
  520.  
  521. /*-----------------------------------------------------------------------------
  522.   *
  523.   * Mask Buffer Class
  524.   *
  525.   *-----------------------------------------------------------------------------
  526.   *
  527.   * The Mask Buffer Class houses all the internal buffer mechanisms asnd
  528.   * provides a cleaner interface for working with buffers.
  529.   *
  530.   *///--------------------------------------------------------------------------
  531. var MaskBuffer = function(m, p){
  532.  
  533. /*-------------------------------------------------------------------------
  534.   *
  535.   * isFixedCharacter
  536.   *
  537.   *-------------------------------------------------------------------------
  538.   *
  539.   * Determines if the postion within the passed in mask is a fixed
  540.   * character or if it is a mask character
  541.   *
  542.   * @param m - the mask
  543.   * @param postion - position to check
  544.   *
  545.   *///----------------------------------------------------------------------
  546. this.isFixedCharacter = function(m, position){
  547. return !availableMasks[m.charAt(position)];
  548. }
  549.  
  550. /*-------------------------------------------------------------------------
  551.   *
  552.   * Generate RegExp
  553.   *
  554.   *-------------------------------------------------------------------------
  555.   *
  556.   * Build up a complete mask regualr expression to validate the enitre
  557.   * input upon blur and paste events.
  558.   *
  559.   *///----------------------------------------------------------------------
  560. var parsedMask = $.map(m.split(""), function(it){
  561. return availableMasks[it] || (/[A-Za-z0-9]/.test(it)?"":"\") + it;
  562. });
  563.  
  564. this.fullRegEx = new RegExp("^" + parsedMask.join("") + "$");
  565.  
  566. /*-------------------------------------------------------------------------
  567.   *
  568.   * Initialize Object
  569.   *
  570.   *-------------------------------------------------------------------------
  571.   *
  572.   * Build the initial buffer, find the first mask character position and
  573.   * store the values internall in this object
  574.   *
  575.   *///----------------------------------------------------------------------
  576. this.start = m.length;
  577. this.buffer = new Array(m.length);
  578.  
  579. for(var i = m.length-1; i >= 0; i--){
  580. if(!this.isFixedCharacter(m,i)){
  581. this.start = i;
  582. this.buffer[i] = p;
  583. }else{
  584. this.buffer[i] = m.charAt(i);
  585. }
  586. }
  587.  
  588. this.initial = $.map(this.buffer, function(e){return e}),
  589. this.mask = m;
  590. }
  591.  
  592. /* Extend the object */
  593. MaskBuffer.prototype = {
  594.  
  595. /*-------------------------------------------------------------------------
  596.   *
  597.   * getInitialMask
  598.   *
  599.   *-------------------------------------------------------------------------
  600.   *
  601.   * Returns a copy of the initial mask array that was used to create
  602.   * this buffer instance. A copy is returned to to prevent any pass by
  603.   * reference overwritting.
  604.   *
  605.   *///----------------------------------------------------------------------
  606. getInitialMask: function(){
  607. return $.map(this.initial, function(e){return e}); // clone
  608. },
  609.  
  610. /*-------------------------------------------------------------------------
  611.   *
  612.   * getStartingMaskPosition
  613.   *
  614.   *-------------------------------------------------------------------------
  615.   *
  616.   * Returns the index of the first mask (i.e. non fixed) character in the
  617.   * mask
  618.   *
  619.   *///----------------------------------------------------------------------
  620. getStartingMaskPosition: function(){
  621. return this.start;
  622. },
  623.  
  624. /*-------------------------------------------------------------------------
  625.   *
  626.   * nextMaskPostion
  627.   *
  628.   *-------------------------------------------------------------------------
  629.   *
  630.   * from the passed in index returns the postion of the next non fixed
  631.   * mask character.
  632.   *
  633.   *///----------------------------------------------------------------------
  634. nextMaskPosition : function(i){
  635. var target = i||0;
  636. while(++target < this.mask.length){
  637. if(!this.isFixedCharacter(this.mask,target)){
  638. return target;
  639. }
  640. }
  641. return this.mask.length;
  642. },
  643.  
  644. /*-------------------------------------------------------------------------
  645.   *
  646.   * get
  647.   *
  648.   *-------------------------------------------------------------------------
  649.   *
  650.   * Returns, depending on the arguments passed, either the string value of
  651.   * the current buffer state of the character at the passed index of the
  652.   * buffer
  653.   *
  654.   *///----------------------------------------------------------------------
  655. get: function(){
  656. return (arguments.length === 0)?this.buffer.join(''):this.buffer[arguments[0]];
  657. },
  658.  
  659. /*-------------------------------------------------------------------------
  660.   *
  661.   * getMaskValue
  662.   *
  663.   *-------------------------------------------------------------------------
  664.   *
  665.   * Returns the character of the mask at the current position
  666.   *
  667.   *///----------------------------------------------------------------------
  668. getMaskValue: function(idx){
  669. return this.mask.charAt(idx);
  670. },
  671.  
  672. /*-------------------------------------------------------------------------
  673.   *
  674.   * size
  675.   *
  676.   *-------------------------------------------------------------------------
  677.   *
  678.   * Returns the length of the buffer/mask/initial mask etc
  679.   *
  680.   *///----------------------------------------------------------------------
  681. size: function(){
  682. return this.buffer.length;
  683. },
  684.  
  685. /*-------------------------------------------------------------------------
  686.   *
  687.   * set
  688.   *
  689.   *-------------------------------------------------------------------------
  690.   *
  691.   * Sets either the enitre buffer or a specific character of the buffer.
  692.   *
  693.   * @param i Array|Number. The array to set the buffer to or the postion
  694.   * of the character to set the value of
  695.   * @param v Boolean|Character. If boolean then it makes determines if a
  696.   * clone of the array is to be used. If character it is the
  697.   * character to put in the current position of the buffer;
  698.   *
  699.   *///----------------------------------------------------------------------
  700. set: function(i,v){
  701. if(i.constructor === Array){
  702. this.buffer = (v)?$.map(i, function(e){return e}):i;
  703. }else{
  704. this.buffer[i] = v;
  705. }
  706.  
  707. },
  708.  
  709. /*-------------------------------------------------------------------------
  710.   *
  711.   * reset
  712.   *
  713.   *-------------------------------------------------------------------------
  714.   *
  715.   * Resets, returns back to the inital mask, all/some/one part of the
  716.   * current buffer depending on inputs
  717.   *
  718.   *///----------------------------------------------------------------------
  719. reset: function(){
  720. var start, end;
  721.  
  722. switch(arguments.length){
  723. case 0 : start = 0; end = this.buffer.length; break;
  724. case 1 : start = end = arguments[0]; break;
  725. case 2 : start = arguments[0]; end = arguments[1]; break;
  726. }
  727.  
  728. for(var i = start; i <= end; i++){
  729. this.buffer[i] = this.initial[i];
  730. }
  731. },
  732.  
  733. /*-------------------------------------------------------------------------
  734.   *
  735.   * test
  736.   *
  737.   *-------------------------------------------------------------------------
  738.   *
  739.   * Test current buffer state against the complete RegExp of the field to
  740.   * determine if it is invliad/incomplete
  741.   *
  742.   *///----------------------------------------------------------------------
  743. test: function(){
  744. return this.fullRegEx.test(this.buffer.join(''));
  745. }
  746. }
  747.  
  748. /*-----------------------------------------------------------------------------
  749.   *
  750.   * Public API
  751.   *
  752.   *-----------------------------------------------------------------------------
  753.   *
  754.   * Extend the core jQuery object to allow access to the public functions
  755.   * for the plugin functionality
  756.   *
  757.   *///--------------------------------------------------------------------------
  758. $.extend({
  759.  
  760. /* $.fn.mask
  761.   * $.fn.unmask
  762.   * $.applyMasks
  763.   */
  764.  
  765. mask : {
  766.  
  767. //-- CONFIGURATION ACCESSORS ------------------------------------------
  768. availableMasks : availableMasks,
  769. options : defaultOptions,
  770.  
  771. /*---------------------------------------------------------------------
  772.   *
  773.   * Unistall plugin
  774.   *
  775.   *---------------------------------------------------------------------
  776.   *
  777.   * Unistall the entire plugin by deregistering all events and data
  778.   * caches in the document but also delete the objects from memory.
  779.   *
  780.   *///------------------------------------------------------------------
  781. uninstall: function(){
  782.  
  783. /* UNMASK */
  784. $.annotated("@Mask", document).unmask();
  785.  
  786. /* GARBAGE COLLECT */
  787. delete(availableMasks);
  788. delete($.applyMasks);
  789. delete($.fn.mask);
  790. delete($.fn.unmask);
  791. delete(defaultOptions);
  792. delete(Carat);
  793. delete(MaskBuffer);
  794. delete($.carat);
  795. delete($.mask);
  796. }
  797. },
  798.  
  799. carat : {
  800. get : Carat.getCaratPosition,
  801. set : Carat.setCaratPosition
  802. }
  803. });
  804.  
  805. })(jQuery);

以上是关于InputMask jQuery插件的主要内容,如果未能解决你的问题,请参考以下文章

InputMask jQuery插件

使用 jquery.inputmask 插件进行 parsley.js 验证

如何使用 jquery inputmask 插件设置文本字段长度的范围

当屏蔽字段具有“只读”属性时,jQuery Inputmask 插件会阻止表单提交

如何使 jquery.inputmask 与输入类型 = 数字一起使用?

jquery.inputmask 3.3.4 的日期时间格式错误?