I need a JQuery IP Mask plugin

前端 未结 5 1307
春和景丽
春和景丽 2021-02-04 09:11

Is there a good IP Mask plugin for JQuery? I\'ve tried Masked Input Plugin but it doesn\'t IP Addresses with less than 12 digits. Then I\'ve tried meioMask and this doesn\'t wor

5条回答
  •  遥遥无期
    2021-02-04 10:09

    i found this and you don´t need to install plugins

    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 255){
                    return false;
                }
            }
            return true;
        } else {
            return false;
        }
    }
    

提交回复
热议问题