代码之家  ›  专栏  ›  技术社区  ›  HyderA

我需要一个jqueryip掩码插件

  •  13
  • HyderA  · 技术社区  · 16 年前

    JQuery有好的IP掩码插件吗?我试过了 Masked Input Plugin meioMask 小于12位数也不行。有什么建议吗?

    5 回复  |  直到 16 年前
        1
  •  9
  •   Philippe    16 年前
        2
  •  3
  •   Destreyf    13 年前

    这是一篇比较老的文章,但是对于那些想要一个简单的方法来操作多个输入的人来说,不需要使用一个庞大的插件,也不需要担心文档或方法,这里有一个简单的类选择器方法可以帮你完成这一切。它只是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函数,您可以使用以下代码段(未测试)

    //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
    });
    

    该代码段的设计目的不是要分离,也不支持IPv6,如果您需要IPv6支持,请参阅 https://code.google.com/p/jquery-input-ip-address-control/ 这是anyulled建议的。

        3
  •  2
  •   Anyul Rivas    14 年前
        4
  •  1
  •   Sphvn Frebin Francis    16 年前

    屏蔽输入插件的工作示例-
    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水印- http://code.google.com/p/jquery-watermark/

        5
  •  1
  •   Santiago Elvira Ramirez    13 年前

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

    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;
        }
    }