0

I am trying to validate an IP address using core javascript. I would like to make sure that each IP address section value must be greater>100. But it is not working properly. Please find the following code for more details.

function testIP(){
            var ip=document.getElementById("ipaddress").value;
            var first= ip.substring(0,3);
            var second= ip.substring(4,7);
            var third= ip.substring(8,11);
            var fourth= ip.substring(12,15);
            var error=false;
            if(first){

                var ippart1 = 0+first;

                if(first<100){

                }
                else{
                    alert("Please enter a valid ip address.First part of ip address is incorrect"); 
                    error=true;
                }
            }
            else    
                error=true;
            if(second){

                var ippart2 = 0+second;

                if(ippart2<100){

                }
                else{
                    alert("Please enter a valid ip address.Second part of ip address is incorrect");
                    error=true;
                }
            }
            else    
                error=true;
            if(third){

                var ippart3 = 0+third;

                if(ippart3<100){

                }
                else{
                    alert("Please enter a valid ip address.Third part of ip address is incorrect"); 
                    error=true;
                }
            }
            else    
                error=true;
            if(fourth){

                var ippart4 = 0+fourth;
                if(ippart4<100){

                }
                else{
                    alert("Please enter a valid ip address.Forth part of ip address is incorrect");
                    error=true;
                }
            }
            else    
                error=true;
            if(error==true){

                return false;
            }
            else
                return true;

        }

I think the problem is in the string to integer conversion. I have also tried parseInt function. It is also not working. Please take a look at it.

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Technosavia
  • 85
  • 2
  • 12
  • Maybe you could use some regex? http://stackoverflow.com/a/5284410/1331430 – Fabrício Matté Feb 02 '13 at 14:44
  • What exactly is the problem? When asking questions, it's very important to say what you expect to happen, and what is happening instead. – Pointy Feb 02 '13 at 14:45
  • What's the error you're getting? – Leniel Maccaferri Feb 02 '13 at 14:45
  • I am looking to find out the issue in this code. Please don't suggest to go away from it. As I developer I would like to solve an existing issue before jump to another solution. Thanks for your time and suggestion. – Technosavia Feb 02 '13 at 14:46
  • I would like to make sure that each ip adresss section must have a value greater than 99. – Technosavia Feb 02 '13 at 14:48
  • 1
    I suggested another answer as your code could do with some (major) refactoring. I'd use `split` as Geliş suggested, then check if the array has 4 items, iterate over them, covert to number with the unary `+` operator and do your check. – Fabrício Matté Feb 02 '13 at 14:54
  • While you're at it, make it flexible to check IPv6 addresses as well. – जलजनक Feb 02 '13 at 15:25

2 Answers2

3

You should split() (http://www.w3schools.com/jsref/jsref_split.asp) the values using dot. And then you can check the values.

Why is your way wrong;

Consider I entered 85.15.13.25

variable values:

first: 85.
second: 15.
third: 13.
fourth: 25

so first, second and third variables are wrong, they contain a dot.

Can Geliş
  • 1,454
  • 2
  • 10
  • 19
  • 1
    [Please be careful with w3schools recommendations.](http://w3schools.com) But this is not a bad idea :-) – Pointy Feb 02 '13 at 14:48
  • Thanks for your support. I have tested the code with an alert. The first,second and third values. All are printed perfectly with out dots. – Technosavia Feb 02 '13 at 14:49
  • @Technosavia - it depends on which numbers you put it. Which numbers did you try? Your code is assuming that each part of the IP is 3 digits... – Lee Taylor Feb 02 '13 at 14:52
  • I have tried with 099.100.100.100. But I did not get the first part error message. Thanks – Technosavia Feb 02 '13 at 14:53
  • 099 is 3 digits long. What if you put 99.100.100.100 ?! – Lee Taylor Feb 02 '13 at 14:53
  • Actually I have used a mask plugin there . So the possible value there will be 099. So please try to find out the solution with that 099 number. Thanks – Technosavia Feb 02 '13 at 14:55
  • substring parts are wrong. `second = ip.substring(4,7);` refers to **.10** according to 099.100.100.100 – Can Geliş Feb 02 '13 at 14:58
  • 1
    @Technosavia does your mask plugin use any placeholder characters? Just wondering as that'd affect number conversion if you don't fill out the entire mask. – Fabrício Matté Feb 02 '13 at 14:59
  • @Andreas I make that mistake all the time :( :( :( – Pointy Feb 02 '13 at 15:00
1

First of all I would suggest to check if the address is valid using this function:

   function validate( value ) {
         RegE = /^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$/  
         if(value.match(RegE))  
            alert('Valid IP');  
         else  
            alert('Invalid IP'); 
      }

Then use parse int but with a good condition (> not <):

if(parseInt(first)>100){

                }

Also consider refactoring your code :)

MiGro
  • 1,471
  • 10
  • 8
  • **this is way wrong**, it would allow things like `999.999.999.999` which is not a valid IP address. `^((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]?)$` will get you much better results! –  Oct 10 '15 at 18:51