最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS Validation IP:Port - Stack Overflow

programmeradmin1浏览0评论

I have a question, on how to validate IP:Port together. example:

192.158.2.10:80 <--Valid

192.158.2.10 <---Invalid

So the port is a must, I found some IP validation(Regex) but to be combind with port no luck. I dont want to use a seperate input field for port.

My Idea was to like so:

var str = '192.168.10.2:80';
var substr = ':';
     if (str.indexOf(substr) !== -1){
         var pieces = str.split(':', 2);
         var ip    = pieces[0];
         var port  = pieces[1];
         //and here validate ip and port
     }else{
         console.log('the char '+substr+' is not there');
     }

Is this right way? or there more simple?

I have a question, on how to validate IP:Port together. example:

192.158.2.10:80 <--Valid

192.158.2.10 <---Invalid

So the port is a must, I found some IP validation(Regex) but to be combind with port no luck. I dont want to use a seperate input field for port.

My Idea was to like so:

var str = '192.168.10.2:80';
var substr = ':';
     if (str.indexOf(substr) !== -1){
         var pieces = str.split(':', 2);
         var ip    = pieces[0];
         var port  = pieces[1];
         //and here validate ip and port
     }else{
         console.log('the char '+substr+' is not there');
     }

Is this right way? or there more simple?

Share Improve this question asked Jan 11, 2014 at 9:25 SergkeiMSergkeiM 4,1686 gold badges40 silver badges73 bronze badges 2
  • Port is always 80 or want any ports? – YOU Commented Jan 11, 2014 at 9:51
  • any port from 1 to 65535 – SergkeiM Commented Jan 11, 2014 at 9:54
Add a comment  | 

5 Answers 5

Reset to default 14

A regular expression would have to be ridiculously long in order to validate that the numbers fall within the acceptable range. Instead, I'd use this:

function validateIpAndPort(input) {
    var parts = input.split(":");
    var ip = parts[0].split(".");
    var port = parts[1];
    return validateNum(port, 1, 65535) &&
        ip.length == 4 &&
        ip.every(function (segment) {
            return validateNum(segment, 0, 255);
        });
}

function validateNum(input, min, max) {
    var num = +input;
    return num >= min && num <= max && input === num.toString();
}

Demo jsfiddle.net/eH2e5

You can simply use the Regex below to validate IP:Port only

Valid IP Address (0.0.0.0 - 255.255.255.255): Valid port (1-65535)

/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):(6553[0-5]|655[0-2][0-9]|65[0-4][0-9][0-9]|6[0-4][0-9][0-9][0-9][0-9]|[1-5](\d){4}|[1-9](\d){0,3})$/

I think '[0-9]+.[0-9]+.[0-9]+.[0-9]+:[0-9]+' might work as well

You can test it at http://regex101.com/

Perhaps this might work. It did in my preliminary tests

var id = '192.158.2.10:80'; // passes - true
// var id = '192.158.2.10'; // fails - false

/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}\:[0-9]{1,3}$/.test(id);

This method explained here uses a regular expression that is more complete:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
function ValidateIPaddress(ipaddress)   
    {  
     if (/^(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]?)$/.test(myForm.emailAddr.value))  
      {  
        return (true)  
      }  
    alert("You have entered an invalid IP address!")  
    return (false)  
    }
</script>

发布评论

评论列表(0)

  1. 暂无评论