2013-09-27 47 views
0

我有2个文本区域,我想应用遮罩。jquery遮罩的邮政编码插件

  1. 文本区域1:多用逗号和空间
  2. 文本区域2分离5位邮政编码:多个3位拉链通过在这两种情况下一个逗号和空间

所以分离代码,允许的字符是0-9和逗号和空格。

我很难为此提出掩蔽。我可以用蒙面插件做这样的事吗?

http://digitalbush.com/projects/masked-input-plugin/

我跟着this拿出一个自定义插件,允许特定的密钥,但得到了与逗号和Ctrl + V的问题。逗号和<都具有相同的密钥代码,所以现在采取掩蔽路线。

//Multiple zip codes separated by comma and space 
jQuery.fn.multipleZipCodesSeparatedByCommaAndSpaceOnly = function() { 
    return this.each(function() { 
     $(this).keydown(function (e) { 
      var key = e.which || e.keyCode; 
      //alert(String.fromCharCode(key)); 
      if (!e.altKey && e.ctrlKey && //&& !e.shiftKey && 
      // numbers 
       (key >= 48 && key <= 57) || 
      // Numeric keypad 
       (key >= 96 && key <= 105) || 
      // comma, space 
       key == 188 || key == 32 || 
      // Backspace and Tab 
       key == 8 || key == 9 || 
      // Home and End 
       key == 35 || key == 36 || 
      // left and right arrows 
       key == 37 || key == 39 || 
      // Del and Ins 
       key == 46 || key == 45) { 
       return true; 
      } 

      return false; 
     }); 
    }); 
}; 

回答

1

下面是我用掩蔽的输入数字仅

$('.numeric').keyup(function() { 
    $(this).val($(this).val().replace(/[^0-9.]/g,'')) 
}); 

所以,改变正则表达式应该可以帮助你实现你想要做什么的代码。 我不是正则表达式专家,但有大量的在线资源!

通过向要屏蔽的输入字段添加一个“数字”类来使用此功能 - 显然请将类名更改为更合适的内容!

+0

谢谢,我把下面这样我可以正常格式化我的实现。 –

0

这里是插件...

//Multiple zip codes separated by comma and space 
jQuery.fn.multipleZipCodesSeparatedByCommaAndSpaceOnly = function (method) { 

    var methods = { 
     init: function() { 
      //keyup 
      $(this).bind('input propertychange', function (e) { 
       methods.ZipCodeHandle(e, $(this)); 
      }); 
     }, 
     ZipCodeHandle: function (e, $object) { 
      $object.val($object.val().replace(/[^\d\, ]/g, '')); 
     } 
    }; 

    return this.each(function() { 
     // Method calling logic 
     if (methods[method]) 
      return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); 
     else if (typeof method === 'object' || !method) 
      return methods.init.apply(this, arguments); 
     else 
      $.error('Method ' + method + ' does not exist'); 
    }); 

};