Regular expression for IP Address Validation

Go To StackoverFlow.com

21

i want to validate the value is valid IP Address or not..!

I Used to validate like

ValidIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";

it's working fine, but when i give the values like 12345678 , its also return true.. How to solve this?

2012-04-04 07:10
by Manikandan Sethuraju
"2001:0db8:85a3:0000:0000:8a2e:0370:7334" isnt a valid IP according to that regex either. IPv6 is the new standard : - TJHeuvel 2012-04-04 07:12
Just a tip: Many answers answer below utilize \d which is more compact than [0-9], but it is more efficient for the regex engine if you use [0-9]. See https://stackoverflow.com/questions/1662173 - Marathon55 2017-12-26 16:05
As I am sure many people are aware, the 4 octet dotted decimal is not the ONLY valid IPv4 format. Google's 8.8.8.8 server can be reached via octal 0010.0010.0010.0010, hex 0x08.0x08.0x08.0x08 and any number of other formats. Most IPv4 implementations will also accept missing octets as valid. So, 8.8 becomes 8.0.0.8 - yes, really. Lastly, if you're testing for "validity", are you concerned if it's an RFC1918 ("local") address, a multicast or some other probably-not-intended address (e.g. Class C broadcast .255 or network .o)? This discussion may help https://superuser.com/questions/92915 - Mark Bradley 2018-09-05 15:04


31

There is a simpler way. You just need to split the string on . and check that every number is between 0 and 255.

Additionally, you can check for hexa and split on : for IPv6.


Just because I think it's funny:

^(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))$

Here is a regex that should handle IPs (v4).

2012-04-04 07:13
by Colin Hebert
+1 simpler and more readable than a humongous rege - Andreas Wong 2012-04-04 07:17
They dont have to be humongous. I bet your code is longer then a good regex - TJHeuvel 2012-04-04 07:19
@TJHeuvel, and yet more performant :). Regexes are a useful tool, but it's a big engine used to do way more complicated tasks, so using one here is like using a bazooka to catch a fly. You'll probably get it done, but at what cost - Colin Hebert 2012-04-04 07:21
I think this one removes redundancy from your version... (?<!\S)((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\b|\.\b){7}(?!\S)Srichakradhar 2013-12-25 18:09
This is a good example where regular expressions can parse the text but cannot understand the semantic meaning of them. You cant easily tell with a regex that the number has to be between 0 and 255 as it cant understand numbers only text. I would capture the regex with ([0-9]+).([0-9]+).([0-9]+).([0-9]+) and validate it without a regular expressio - Har 2014-07-15 12:03
how would you prevent the first number from being 0 - user2878881 2015-11-26 12:48
I think the splitting an IPv4 address by . approach lends itself better for testing "meaningful" addresses, such as not RFC1918, not multicast and potentially not Class A/B/C broadcast (e.g. .255) or network (e.g. ,0) addresses. If the aim is validation of user input, not only would you want to protect from malicious input, but also user, er, "ignorance". If someone enters an RFC1918 address (e.g. 192.168.1.2) on a public server, that could be both a security breach (depending how the address is used), but probably also not what the user desired - Mark Bradley 2018-09-05 15:10


12

Looking for one for IPv4, I ended up just creating it myself. (This only handles the common dotted variant, i.e. 0.0.0.0 - 255.255.255.255)

^                           # START OF STRING
  (?=\d+\.\d+\.\d+\.\d+$)     # Lookahead, require this format: number.number.number.number END OF STRING
  (?:                         # Start non-capture group (number 0-255 + optional dot)
    (?:                         # Start non-capture group (number 0-255)
      25[0-5]                     # 250-255
      |                           # OR
      2[0-4][0-9]                 # 200-249
      |                           # OR
      1[0-9]{2}                   # 100-199
      |                           # OR
      [1-9][0-9]                  # 10-99
      |                           # OR
      [0-9]                       # 0-9
    )                           # End non-capture group
    \.?                         # Optional dot (enforced in correct positions by lookahead)
  ){4}                        # End non-capture group (number + optional dot), repeat 4 times
$                           # END OF STRING

Without comments:

^(?=\d+\.\d+\.\d+\.\d+$)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.?){4}$

Some code to test it:

function isValidIpv4Addr(ip) {
  return /^(?=\d+\.\d+\.\d+\.\d+$)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.?){4}$/.test(ip);
}
var testAddr = ['192.68.35.35','0.0.0.0','255.0.0.0','192.168.1.0','192.168.0.1','255.255.255.0','1.1.1.1','255.255.255.255','249.249.249.249','200.200.200.200','199.199.199.199','100.100.100.100','99.99.99.99','0.0.0.0','9.9.9.9','10.10.10.10','99.99.99.99','100.100.100.100','109.109.109.109','110.110.110.110','199.199.199.199','200.200.200.200','249.249.249.249','250.250.250.250','255.255.255.255','256.256.256.260','192.168.0.0/24','192.168..1','192.168.1','1','1.','1.1','1.1.','1.1.1','1.1.1.','1.1.1.1.','1.1.1.1.1','.1.1.1.1','01.01.01.01','09.09.09.09','1.0.0.1.0','010.1.1.1','123456','123123123123','.127.0.0.1'];
for (var i = 0; i < testAddr.length; i++) {
  document.getElementById('ipv4tests').innerHTML += '<li>' + testAddr[i] + ' ' + (isValidIpv4Addr(testAddr[i]) ? '<font color="green">VALID!</font>' : '<font color="red">INVALID!</font>') + '</li>';
}
<ul id="ipv4tests"></ul>

2014-10-31 09:19
by ohaal
This incorrectly matches 256.256.256.259 - https://regex101.com/r/vX2hK4/1 - Nick Grealy 2015-02-17 06:12
Good catch, thank you! Fixed it now. Guess I was a bit too sloppy on the tests - ohaal 2015-02-17 09:21
Excellent write-up explaining what the regex does. Here's a regex based on yours that will also match CIDR notation (not perfect, but close enough): ^(?=\d+\.\d+\.\d+\.\d+(\/\d+)?$)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.?){4}(?:\/(?:[0-9]|1[0-9]|2[0-9]|3[0-2]))?$Nathan 2016-03-25 16:22


11

This works properly for all possible cases.

^(([1-9]?\d|1\d\d|2[0-5][0-5]|2[0-4]\d)\.){3}([1-9]?\d|1\d\d|2[0-5][0-5]|2[0-4]\d)$
2013-10-21 15:10
by kirill.buga
.The 2[0-5][0-5] part can be modified as 25[0-5] since 2[0-4]\d matches everything from 200 up to 249. A little shorter version of your answer would be ^(([1-9]?\d|1\d\d|25[0-5]|2[0-4]\d)\.){3}([1-9]?\d|1\d\d|25[0-5]|2[0-4]\d)$ Great answer, btw - Srichakradhar 2013-12-25 05:45
I would normally distrust something that claims to work in all possible cases. Did you run infinite tests? Or derive a mathematical proof? However, in this case I think this actually is the best solution, and probably does work correctly in all cases : - scentos 2015-02-16 12:17
Here's a fiddle with some tests for this regex, to prove it works: http://refiddle.com/27u - Jon Schneider 2015-03-18 20:34


6

I know this is old, but try this one:

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

I made it today for a function in php.

It handles ip's from 0.0.0.0 to 255.255.255.255 and ports from 0 to 65535.

Examples:

validates:
    0.0.0.0:0
    255.0.0.0
    192.168.1.0:8080
does not validate:
    192.168.0.0/24
    192.168..1
    192.168.1

I know this is a frankenregex, but still, it works!

If port doesn't matter, use this one:

    /^(?:(?:2[0-4]\d|25[0-5]|1\d{2}|[1-9]?\d)\.){3}(?:2[0-4]\d|25[0-5]|1\d{2}|[1-9]?\d)$/
2013-12-26 23:39
by Ismael Miguel
Top notch mate, thanks for the post - David G 2014-05-13 00:26
@DavidG You are welcome. Anything you see that can be improved, please, place a comment here - Ismael Miguel 2014-05-13 08:13
Mate, very nice work! Of the four answers that correctly match this test https://regex101.com/r/vX2hK4/14 , yours is the most performant by far! http://jsperf.com/js-regex-ip-address (puts my answer to shame - Nick Grealy 2015-02-17 06:26
@NickG I never knew that Regex could be THAT fast! o.O I would also add the version without port number. I will be testing it on different browsers and systems. And thank you for taking your time to write a testcase - Ismael Miguel 2015-02-17 09:18


3

Try this shortened one:

^(([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])(\.(?!$)|(?=$))){4}$

Here is the test case for this regex:

  function verifyIp(ip)
  {
    return /^(([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])(\.(?!$)|(?=$))){4}$/.test(ip||"");
  }
  
  ["192.68.35.35","0.0.0.0","255.0.0.0","192.168.1.0","192.168.0.1","255.255.255.0","1.1.1.1","255.255.255.255","249.249.249.249","200.200.200.200","199.199.199.199","100.100.100.100","99.99.99.99","0.0.0.0","9.9.9.9","10.10.10.10","99.99.99.99","100.100.100.100","109.109.109.109","110.110.110.110","199.199.199.199","200.200.200.200","249.249.249.249","250.250.250.250","255.255.255.255","256.256.256.260","192.168.0.0/24","192.168..1","192.168.1","1","1.","1.1","1.1.","1.1.1","1.1.1.","1.1.1.1.","1.1.1.1.1",".1.1.1.1","01.01.01.01","09.09.09.09","1.0.0.1.0","010.1.1.1","123456","123123123123",".127.0.0.1"].forEach(function(item){
    is_valid = verifyIp(item);
    $('<div>'+item+' <span class="'+(is_valid?'correct':'wrong')+'">'+(is_valid?'VALID':'INVALID')+'</span></div>').appendTo('#result');
  });
.item {
  font-weight: bold;
}
.wrong {
  color: red;  
}

.correct {
  color: green;  
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="result"></div>

2016-05-24 11:43
by Slavik Meltser


1

Here is solution:

^(([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5]))\.){3}([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5]))$
2014-05-19 10:53
by Prakash Thapa
This incorrectly matches zero prefixed numbers 010.1.1.1 - https://regex101.com/r/vX2hK4/1 - Nick Grealy 2015-02-17 06:14


1

You might also try this:

^((?:(?:^|\.)(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){4})$

We want the pattern to repeat exactly four times - in this case our pattern is a number in the range of 0 - 255 preceded by either a period . or the start of the string! Since the start of the string can occur only once, the other three occurrences must be periods.

Please see this Regex 101 demo for a full explanation.

2015-02-11 02:47
by David Faber
Incorrectly matches when there is a leading dot, e.g. .1.1.1.1scentos 2015-02-16 12:06
@scentos, thanks for the correction. Can add a negative lookeahead to address that: (?!^\.): https://regex101.com/r/vX2hK4/1 - David Faber 2015-02-16 18:38


1

Just extending on @DavidFaber 's excellent solution. To match an IPv4 "Dotted decimal" notation (no range/ports):

^(((1?[1-9]?|10|2[0-4])\d|25[0-5])($|\.(?!$))){4}$

Match examples: https://regex101.com/r/vX2hK4/15

Code golf anyone?

2015-02-11 03:31
by Nick Grealy
This extended solution incorrectly matches .127.0.0.1scentos 2015-02-16 12:02
Good catch @scentos, solution updated to exclude . prefixed addresses - Nick Grealy 2015-02-16 12:50


1

This reg ex works well but trust me its an overkill.
To have conditional comparisons like here less then 255 its best to have combination of RegEx and conditionals.

^(([0-1]?[0-9]?[0-9]{1})|(2?[0-4]?[0-9]{1})|(25[0-5]))\.(([0-1]?[0-9]?[0-9]{1})|(2?[0-4]?[0-9]{1})|(25[0-5]))\.(([0-1]?[0-9]?[0-9]{1})|(2?[0-4]?[0-9]{1})|(25[0-5]))\.(([0-1]?[0-9]?[0-9]{1})|(2?[0-4]?[0-9]{1})|(25[0-5]))$
2015-05-16 18:23
by Rishul Matta


1

you can simply use this regex to validate any ip address without port number, like this format (192.168.1.1)

/^(?:(?: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]?)$/
2017-07-11 07:31
by Raad Altaie


0

TRY THIS,

ValidIpAddressRegex = "^(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]?)$"
2016-01-11 17:19
by NIMISHAN


0

trying to shorten Grealy's version

^((1?\d?\d|2[0-4]\d|25[0-5])($|\.(?!$))){4}$

note: as the previous version this doesn't correctly handle octal numbers, like 0177.0.0.1

2016-02-10 12:47
by rejetto


0

You can also check with my given expressions as well, I had checked and written a program in java to validate the ipv4 address. It returns true if Ipv4 address is correct or vice-versa.

String pattern="^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])$"

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Scanner;

class Solution{

    public static void main(String []args){
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            String IP = in.next();
            System.out.println(IP.matches(new MyRegex().pattern));
        }

    }
}

    class MyRegex{
        String pattern="^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\dCongrats, you solved this challenge!\\d?|2[0-4]\\d|25[0-5])$";

}
2016-11-22 19:35
by Mukesh Kumar Gupta


0

Colin Hebert pointed out the best solution. But no one have "explained" by supplying the code for it, so here goes ( "Just because I think it's funny:" ;)

var aIP = [
  '192.168.0.1',
  '255.255.255.255',
  '1.2.34.647',
  '256.0.0.0',
  '255,0,0,0',
  '123.123.123',
  '1.2.3.4.5'
  ];

aIP.forEach(function(ipAddr) {
  
  var a = ipAddr.split('.'),
      cnt = 4;

  document.write('Testing ' + ipAddr + '<br/>');

  try {
    a.forEach(function(v) {
      if( v<0 || v>255 )
        throw false;
      cnt--;
    });
    if( cnt!=0 )
        throw false;
      cnt--;
    document.write('- Pass!<br/>');
  }
  catch (e) {
    document.write('- Fail!<br/>');
  }
});

2016-12-02 15:06
by SamWhan


0

Try this as well:

(((?<![\d])([0-9][\.])|(?<![\d])([1-9][0-9][\.])|(?<![\d])(1[0-9]{2}[\.])|(?<![\d])(2[0-5][0-5][\.]))(([0-9][\.])|([1-9][0-9][\.])|(1[0-9]{2}[\.])|(2[0-5][0-5][\.])){2}(([0-9])(?![\d])|([1-9][0-9])(?![\d])|(1[0-9]{2})(?![\d])|(2[0-5][0-5])(?![\d])))

although this is a 5 years old question so I doubt you are still looking for the answer.

posted more info on another thread: Validating IPv4 addresses with regexp

2017-09-13 23:49
by rda


0

for the pattern ex 192.168.23.28/255.255.255.0

^(25[0-5]|2[0-4][0-9]|[01]?[1-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-4]|2[0-4][0-9]|[01]?[1-9][0-9]?)\/(((128|192|224|240|248|252|254)\.0\.0\.0)|(255\.(0|128|192|224|240|248|252|254)\.0\.0)|(255\.255\.(0|128|192|224|240|248|252|254)\.0)|(255\.255\.255\.(0|128|192|224|240|248|252|254)))$

for the pattern ex 192.168.26.82/24 or 192.168.23.28/255.255.255.0

^(25[0-5]|2[0-4][0-9]|[01]?[1-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-4]|2[0-4][0-9]|[01]?[1-9][0-9]?)\/([1-9]|1[0-9]|2[0-9]|3[0-2]|(((128|192|224|240|248|252|254)\.0\.0\.0)|(255\.(0|128|192|224|240|248|252|254)\.0\.0)|(255\.255\.(0|128|192|224|240|248|252|254)\.0)|(255\.255\.255\.(0|128|192|224|240|248|252|254))))$

for the pattern ex 192.168.26.28

^(25[0-5]|2[0-4][0-9]|[01]?[1-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-4]|2[0-4][0-9]|[01]?[1-9][0-9]?)$

for the netmask ex 255.255.255.0

^(((128|192|224|240|248|252|254)\.0\.0\.0)|(255\.(0|128|192|224|240|248|252|254)\.0\.0)|(255\.255\.(0|128|192|224|240|248|252|254)\.0)|(255\.255\.255\.(0|128|192|224|240|248|252|254)))$
2018-03-23 13:23
by Shashank Bhatnagar


0

The OP asked for validation of an IP address. The phrasing of the Q almost certainly implies IPv4 (as opposed to IPv6). I've already commented on the OP about how far that validity checking might go, as well as applauding one responder for taking an non-RE approach. As I also wanted to (under some circumstances) test for validity for the address if I use it on a public server, I came up with the following JS:

function isSimpleIPv4( ip, u=true ) {
    if ((ip === undefined) || (ip === null) || (ip.length > 15)) return false;
    var p = ip.split(\'.\');
    if (p.length != 4) return false;
    p.forEach( function(v,k){p[k]=Number(v);} );
    if (isNaN(p[0]) || isNaN(p[1]) || isNaN(p[2]) || isNaN(p[3]) ) return false;
    if ((p[0] < 1) || (p[0] > 255) || (p[1] < 0) || (p[1] > 255) || (p[2] < 0) || (p[2] > 255) || (p[3] < 0) || (p[3] > 255)) return false;
    if (!u) return true;
    if ((p[0] > 223)) return 'multicast';
    if ((p[0] == 127)) return 'loopback';
    if ((p[0] == 10)) return 'RFC1918';
    if ((p[0] == 192) && (p[1] == 168)) return 'RFC1918';
    if ((p[0] == 172) && (p[1] >= 16) && (p[1] <= 31)) return 'RFC1918';
    return true;
}

If one wants to check for "useful" addresses, then only the string IP is needed, otherwise, if just checking for 0-255.0-255.0-255.0-255, then call the function with the IP-string and false. In the former case, the function will return true/false/ when is the "failing" cause of the usefulness. RFC1918 addresses intended by a website visitor won't be reachable from a public server and may even compromise one of one's own servers (as might addresses in the loopback range 127.x.x.x). Equally, using a multicast address is not helpful either. If you're interested in usefulness checking but don't care about the cause, then you'll just need to do if (isSimpleIPv4(ipString) !== true) console.log('Not a valid and useful IP address');

2018-09-05 16:01
by Mark Bradley


0

IPv4 has 4 blocks of numbers from 0 to 255 that could contain padding zeroes on the left. Each block is spaced with a dot.

Short and simple IPv4 validator:

var block "([0-1]{0,1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|)";
var ipv4 = "(" + block +"\\.){3}" + block ;

Validates any IP Like:

  • 000.12.12.034
  • 121.234.12.12
  • 23.45.12.56
  • 003.045.012.056
  • 03.45.12.6
  • 0.45.122.255
2019-02-12 11:01
by Dubas


0

    import { Component } from '@angular/core';
    import { FormBuilder, FormGroup, Validators }   from '@angular/forms';

    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.scss'],
    })
    export class AppComponent {
      ipranging="";
      testForm1: FormGroup;
      testForm2: FormGroup;
      constructor(private fb: FormBuilder){

      }
      ngOnInit(): void {
        const ipPattern = 
        "(?:(?: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]?)";
        this.testForm1 = this.fb.group({
          inp: ['', Validators.pattern(ipPattern)],
          inp3: ['', Validators.pattern(ipPattern)]
        });
        this.testForm2 = this.fb.group({
          inp: ['', Validators.pattern(ipPattern)],
          inp2: ['', Validators.pattern(ipPattern)],
          inp3: ['', Validators.pattern(ipPattern)]
        });
        this.testForm2.setValidators(this.comparisionValidator);
      }

      public comparisionValidator(group: FormGroup) : any{

        const control1 = group.controls['inp'];
        const control2 = group.controls['inp2'];
        var control1array  = control1.value.split('.');
        var control2array = control2.value.split('.');

        if(parseInt(control1array[0]) > parseInt(control2array[0]) ){
          group.controls['inp3'].setErrors({ 'value2GreaterThanValue1': true });
          console.log(group); 
        }
        else if(parseInt(control1array[1]) > parseInt(control2array[1]) ){
          group.controls['inp3'].setErrors({ 'value2GreaterThanValue1': true });
          console.log(group); 
        }
        else if(parseInt(control1array[2]) > parseInt(control2array[2]) ){
          group.controls['inp3'].setErrors({ 'value2GreaterThanValue1': true });
          console.log(group); 
        }
        else if(parseInt(control1array[3]) > parseInt(control2array[3]) ){
          group.controls['inp3'].setErrors({ 'value2GreaterThanValue1': true });
          console.log(group); 
        }
        else {
          group.controls['inp3'].setErrors({ 'value2GreaterThanValue1': false });
          console.log(group);
        }   
      }
    }
2019-02-24 00:53
by user11107730
Please explain your code - vs97 2019-02-24 03:08


0

    <div style="text-align:left">
        <h2>Choose if you want to enter a single ip or range of ip's</h2>
      <select  [(ngModel)]="ipranging">
        <option  selected disabled value="none"> -- select an option -- </option>
        <option  value='ip'>Single Ip address</option>
        <option  value="range">Ip range</option>
      </select>
      </div>

    <form *ngIf="ipranging === 'ip'" novalidate [formGroup]="testForm1" class="render">
      <label>IP Address: 
        <input formControlName="inp" placeholder='0.0.0.0'/></label>
            <input formControlName="inp3" hidden/>
          <!-- <p *ngIf="testForm.controls.inp.status == 'INVALID' && testForm.controls.inp.value != ''" >Invalid</p>
          <p *ngIf="testForm.controls.inp2.status == 'INVALID' && testForm.controls.inp2.value != ''" >Invalid</p> -->
          <p *ngIf="testForm1.controls.inp.value != '' && testForm1.controls.inp.status == 'INVALID'" >Invalid</p>
    </form>

    <form *ngIf="ipranging === 'range'" novalidate [formGroup]="testForm2" class="render">
      <label>Starting IP:
        <input formControlName="inp" placeholder='0.0.0.0'/></label>
          <label>
              Ending IP:
            <input formControlName="inp2" placeholder='0.0.0.0'/></label>
            <input formControlName="inp3" hidden/>
          <!-- <p *ngIf="testForm.controls.inp.status == 'INVALID' && testForm.controls.inp.value != ''" >Invalid</p>
          <p *ngIf="testForm.controls.inp2.status == 'INVALID' && testForm.controls.inp2.value != ''" >Invalid</p> -->
          <p *ngIf="testForm2.controls.inp.value != '' && testForm2.controls.inp.status == 'INVALID' || testForm2.controls.inp2.value != '' && testForm2.controls.inp2.status == 'INVALID'" >Invalid</p>
          <p *ngIf="testForm2.controls.inp3.errors.value2GreaterThanValue1 == true">Starting IP is larger than the ending IP</p>
    </form>
2019-02-24 00:58
by user11107730
Code only answers are discouraged. Please add some explanation as to how this solves the problem, or how this differs from the existing answers. From ReviewNick 2019-02-24 02:02


-1

This should work

^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$
2014-03-26 06:27
by kanishka vatsa
Close... https://regex101.com/r/vX2hK4/1 - Nick Grealy 2015-02-17 06:15
No mechanism to impose max value of 25 - Rishul Matta 2015-05-16 18:09
Ads