Technosavia
Technosavia

Reputation: 85

IP address validation not working

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.

Upvotes: 0

Views: 1692

Answers (2)

MiGro
MiGro

Reputation: 1511

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 :)

Upvotes: 1

Can Geliş
Can Geliş

Reputation: 1474

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.

Upvotes: 3

Related Questions