IP地址验证,中间有适当的点 [英] IP address validation with proper dots in between

查看:101
本文介绍了IP地址验证,中间有适当的点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wanted to validate only for IP address accepting only 3 three dots after some numbers

例如: 有效:191.123.121.202有效,它在小数点后有3个点. 无效:191..123.121.202无效,其中连续有2个点

ex: Valid: 191.123.121.202 is valid which has 3 dots after some decimal. Invalid : 191..123.121.202 is invalid where 2 dots are in sequence

Whole Point:需要一个强大的IP验证器

Whole Point: wanted a robust IP validator

$("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,则它应该接受,否则不应允许他粘贴.

TO some extent i have got with the help of some other site. And also wanted if the user copy and paste the correct IP, then it should accept, else it should not allow him to paste.

演示

推荐答案

演示

尝试

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地址.


Update for validating IP address with port numbers.

Ex. 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');
        }
    }
});

工作场所

这篇关于IP地址验证,中间有适当的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆