3

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?

SergkeiM
  • 3,934
  • 6
  • 36
  • 69

5 Answers5

13

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

gilly3
  • 87,962
  • 25
  • 144
  • 176
4

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

Raad Altaie
  • 1,025
  • 1
  • 15
  • 28
  • Does this cater for the max range of ip and port number values? Like the top response handled? – srb633 May 17 '20 at 01:04
  • 1
    It matches a valid usable ip address with any 5 digit port number. – Raad Altaie May 17 '20 at 01:17
  • So the 5 digit port could be above 65535? – srb633 May 17 '20 at 01:22
  • well you need the long regex below to matches valid ip and 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})$` – Raad Altaie May 17 '20 at 01:38
  • Okay great, I honestly don't mind a long regex. I prefer at least that it's regex and not some large function. – srb633 May 17 '20 at 02:16
0

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

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

Bun Suwanparsert
  • 915
  • 2
  • 13
  • 32
  • It worked but if the input will be 234234235235.234234234.234234.234234:234234234 it is still show valid. can we make max 3 character after each "." – SergkeiM Jan 11 '14 at 09:36
  • http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address you should take this – Bun Suwanparsert Jan 11 '14 at 09:47
0

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);
james emanon
  • 11,185
  • 11
  • 56
  • 97
  • It worked just changed the port range from 1-3 to 1-5 character. `/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}\:[0-9]{1,5}$/` Ofcourse more check will be on server side, this is just to show to user that the input is invalid. – SergkeiM Jan 11 '14 at 09:50
  • 3
    999.999.999.999:0 will pass too btw. but, well, its depends on how accurate you want. – YOU Jan 11 '14 at 09:54
0

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>