【问题标题】:I need a JQuery IP Mask plugin我需要一个 JQuery IP 掩码插件
【发布时间】:2011-03-12 03:26:10
【问题描述】:

JQuery 有好的 IP 掩码插件吗?我试过Masked Input Plugin,但它没有少于 12 位的 IP 地址。然后我尝试了meioMask,这也不适用于少于 12 位的数字。有什么建议吗?

【问题讨论】:

  • 你的意思是“不能使用少于 12 位数字”??你能给我举几个例子吗???
  • IPv6 更多我猜是这个问题,所以它不适用于 IPv4?另外,请发布一些示例/代码以帮助您完成工作,如果没有合理的问题,您将无法获得合理的答案。所以我只能说42。
  • 您是否绝对需要屏蔽输入,或者如果输入在单击按钮时得到验证,也可以?
  • @Ozaki:这是一个 jquery 插件,我的代码有什么用?! @Philippe:我已经有一个验证器,我也希望有一个很好的屏蔽输入。
  • @gAMBOOKa 你需要更具体一点,它不能少于 12 位数字不是问题,而是每个字段不能做 1-3 位数字。

标签: javascript jquery jquery-plugins ip-address


【解决方案1】:

你可以在这篇文章中找到你的答案:

http://mlntn.com/2009/12/30/jquery-ip-address-plugin/

还有一个演示供您尝试

http://mlntn.com/demos/jquery-ipaddress/

【讨论】:

【解决方案2】:

这是一篇较旧的帖子,但是对于想要一种简单的方法来操作多个输入的人,而不使用膨胀插件,或者不必担心文档或方法,这里有一个简单的类选择器方法,可以为您完成这一切。它只支持 IPv4,但听起来您的需求很简单。

//jQuery 1.9+ selector pattern,
//To get working with an older version
//Swap first line to $(".ip").bind('keydown',function(e){
//To get working with jQuery versions support .live
//$(".ip").live('keydown',function(e){
$(document).on('keydown',".ip",function(e){
    var code = e.keyCode || e.which;
    var sections = $(this).val().split('.');
    //Only check last section!
    var isInt       = ((code >= 48 && code <= 57) || (code >= 96 && code <= 105));
    var hasSlash    = $(this).val().indexOf("/") == -1;
    if(isInt){
        if(hasSlash){
            if(sections.length < 4){
                //We can add another octet
                var val = parseInt(sections[sections.length-1]+String.fromCharCode(code));
                if(val > 255 || parseInt(sections[sections.length-1]) == 0){
                    $(this).val($(this).val()+"."+String.fromCharCode(code));
                    return false;
                }
                return true;
            } else {
                //Lets prevent string manipulations, our string is long enough
                var val = parseInt(sections[sections.length-1]+String.fromCharCode(code));
                if(val > 255 || parseInt(sections[sections.length-1]) == 0){
                    return false;
                }
                return true;
            }
        } else {
            var cidr_split = $(this).val().split('/');
            var target_val = parseInt(cidr_split[1]+String.fromCharCode(code));
            return (target_val < 33 && target_val.toString().length < 3 && parseInt(cidr_split[1]) != 0);
        }
    } else if(code == 191){
        //CIDR Slash
        return ($(this).val().indexOf("/") == -1);
    } else if(code == 8 || code == 46 || code == 9 || code == 13){
        return true;
    }
    return false
});

为了理解这一点,您在输入中绑定类“ip”,它将自动处理其余部分:D 此版本支持 CIDR 表示法(例如:192.168.1.1/16)它只允许有效地址输入,要删除 CIDR 功能,您可以使用以下 sn -p(未测试)

//jQuery 1.9+ selector pattern,
//To get working with an older version
//Swap first line to $(".ip").bind('keydown',function(e){
//To get working with jQuery versions support .live
//$(".ip").live('keydown',function(e){
$(document).on('keydown',".ip",function(e){
    var code = e.keyCode || e.which;
    var sections = $(this).val().split('.');
    //Only check last section!
    var isInt       = ((code >= 48 && code <= 57) || (code >= 96 && code <= 105));
    if(isInt){
        if(sections.length < 4){
            //We can add another octet
            var val = parseInt(sections[sections.length-1]+String.fromCharCode(code));
            if(val > 255 || parseInt(sections[sections.length-1]) == 0){
                $(this).val($(this).val()+"."+String.fromCharCode(code));
                return false;
            }
            return true;
        } else {
            //Lets prevent string manipulations, our string is long enough
            var val = parseInt(sections[sections.length-1]+String.fromCharCode(code));
            if(val > 255 || parseInt(sections[sections.length-1]) == 0){
                return false;
            }
            return true;
        }
    } else if(code == 8 || code == 46 || code == 9 || code == 13){
        return true;
    }
    return false
});

我在这里提供代码有两个目的 1)这是我认为需要解决的问题,2)我希望为世界做出贡献

sn-p 不是设计用来拆开的,也不支持 IPv6,如果需要 IPv6 支持请看https://code.google.com/p/jquery-input-ip-address-control/anyulled 的建议。

但除了复杂的语法之外,它将八位字节分开,并且只检查“活动”八位字节,它支持任何有效地址(0.0.0.0、0.0.0.0/0 等),所以明智地使用它不会除了防止无效输入之外的任何花哨的检查。如果您正在寻找检查器,请参阅 Santiago Elvira Ramirez 关于 IP 地址验证器的帖子。

【讨论】:

    【解决方案3】:

    【讨论】:

      【解决方案4】:

      来自屏蔽输入插件的工作示例 -
      http://digitalbush.com/projects/masked-input-plugin/

      少于 12 个字符:

      jQuery(function($){
         $("#date").mask("99/99/9999");
         $("#phone").mask("(999) 999-9999");
         $("#tin").mask("99-9999999");
         $("#ssn").mask("999-99-9999");
      });
      

      他们有运行完美的工作示例?

      您的问题是什么,您能否再发布一些深入的信息?

      jQuery(function($){
          $("#MyElementID").mask("10.0.0.0"); //Does this not work?
      });
      

      您是否尝试在每个字段中计算 1-3 位数字?

      例如能够。

      $("#MyElementID").mask("1.0.0.0"); //this
      $("#MyElementID").mask("10.10.10.10"); //or this
      $("#MyElementID").mask("100.100.100.100"); //or this
      

      如果你描述得更详细,你可以得到帮助..

      如果您在此之后,可以尝试通过在输入框上加水印而不是强制使用掩码来尝试更简单的方法,这样您就可以改变可以输入的数字。 见 Jquery-Watermark - http://code.google.com/p/jquery-watermark/

      【讨论】:

      • 是的,我需要在每个字段中输入 1-3 位数,没有具体的位数。而且我不确定水印插件会有什么帮助。
      • 有人请解释下否决的原因?
      • 我没有投反对票,但我需要知道如何允许屏蔽 1-3 位数字。
      【解决方案5】:

      我发现了这个,你不需要安装插件

      function fnValidateIPAddress(ipaddr) {
          //Remember, this function will validate only Class C IP.
          //change to other IP Classes as you need
          ipaddr = ipaddr.replace( /\s/g, "") //remove spaces for checking
          var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/; //regex. check for digits and in
                                                //all 4 quadrants of the IP
          if (re.test(ipaddr)) {
              //split into units with dots "."
              var parts = ipaddr.split(".");
              //if the first unit/quadrant of the IP is zero
              if (parseInt(parseFloat(parts[0])) == 0) {
                  return false;
              }
              //if the fourth unit/quadrant of the IP is zero
              if (parseInt(parseFloat(parts[3])) == 0) {
                  return false;
              }
              //if any part is greater than 255
              for (var i=0; i<parts.length; i++) {
                  if (parseInt(parseFloat(parts[i])) > 255){
                      return false;
                  }
              }
              return true;
          } else {
              return false;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-30
        • 2013-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多