javascript - IP 地址验证,中间有适当的点

标签 javascript jquery logic

我只想验证一些数字后仅接受 3 个三点的 IP 地址

例如: 有效:191.123.121.202 有效,小数点后有 3 个点。 无效 : 191..123.121.202 无效,其中 2 个点按顺序排列

重点:想要一个强大的 IP 验证器

$("input.onlynumberdecimal").keydown(function (event) {

        console.log(event.keyCode);

        if (event.shiftKey == true) {
            event.preventDefault();
        }

        if ((event.keyCode >= 48 && event.keyCode <= 57) || 
            (event.keyCode >= 96 && event.keyCode <= 105) || 
            event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 37 ||
            event.keyCode == 39 || event.keyCode == 46 || event.keyCode == 190) {

        } else {
            event.preventDefault();
        }

        if($(this).val().indexOf('.') !== -1 && event.keyCode == 190)
            event.preventDefault(); 
        //if a decimal has been added, disable the "."-button

    });

在某种程度上,我得到了其他网站的帮助。并且还希望如果用户复制并粘贴正确的IP,那么它应该接受,否则它不应该允许他粘贴。

DEMO

最佳答案

DEMO

试试这个

var pattern = /\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/;
x = 46;
$('input[type="text"]').keypress(function (e) {
    if (e.which != 8 && e.which != 0 && e.which != x && (e.which < 48 || e.which > 57)) {
        console.log(e.which);
        return false;
    }
}).keyup(function () {
    var this1 = $(this);
    if (!pattern.test(this1.val())) {
        $('#validate_ip').text('Not Valid IP');
        while (this1.val().indexOf("..") !== -1) {
            this1.val(this1.val().replace('..', '.'));
        }
        x = 46;
    } else {
        x = 0;
        var lastChar = this1.val().substr(this1.val().length - 1);
        if (lastChar == '.') {
            this1.val(this1.val().slice(0, -1));
        }
        var ip = this1.val().split('.');
        if (ip.length == 4) {
            $('#validate_ip').text('Valid IP');
        }
    }
});

使用端口号验证 IP 地址的更新。

例如。 192.168.2.100:27896

var pattern = /\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\:([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])\b/;
x = 46;
$('input[type="text"]').keypress(function (e) {
    console.log(e.which);
    if (e.which != 8 && e.which != 0 && e.which != x && e.which !=58 && (e.which < 48 || e.which > 57)) {
        console.log(e.which);
        return false;
    }
}).keyup(function () {
    var this1 = $(this);
    if (!pattern.test(this1.val())) {
        $('#validate_ip').text('Not Valid IP');
        while (this1.val().indexOf("..") !== -1) {
            this1.val(this1.val().replace('..', '.'));
        }
        x = 46;
    } else {
        x = 0;
        var lastChar = this1.val().substr(this1.val().length - 1);
        if (lastChar == '.') {
            this1.val(this1.val().slice(0, -1));
        }
        var ip = this1.val().split('.');
        if (ip.length == 4) {
            $('#validate_ip').text('Valid IP');
        }
    }
});

WORKING FIDDLE

关于javascript - IP 地址验证,中间有适当的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17987015/

相关文章:

javascript - 我收到错误 : Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script

javascript - 根据输入值启用按钮

javascript - 在 Highcharts 中设置动态 x 轴范围

绑定(bind)为对象值的函数的javascript返回值

c++ - 如何有效地将 if 和 else 用于过滤结构?

c - 程序段最小化 - if, else

javascript - 递归循环对象以构建属性列表

javascript - 在文本后保持相同的高度和宽度 ('' )

javascript - Jquery Click-handler 不适用于命名函数?

node.js - 到达路线后,您应该将逻辑放在 Express 应用程序中的什么位置?