Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

122
Visualizações
jQuery checking if IP address are within the range

I want to be able to check that two IP address are within the range. So if a user enter something like 10.10.10.0 and 10.10.10.10 then it will pass the validation and enter that address into the database. Then want to check if a users enter something like 10.9.10.10 that would be invalid and I would ask the user to correct the error. My thinking is that I would convert the IP address into long and then make sure the endIpAddrToLong is greater then the startIpAddrToLong

Is this the correct Logic to do something like that? Can I just convert the IP's to longs and compare them? The message I would display to the user would be IP address ranges cannot be defined outside the Address field.

    $('#button-validate').on('click', function() {
       let startIpAddr = $("#txtstartIpAddr").val();
       var startIpAddrToLong = parseInt(IpAddressToLong(startIpAddr));
       
       let endIpAddr = $("#txtendIpAddr").val();
       var endIpAddrToLong = parseInt(IpAddressToLong(endIpAddr));
      
       if (endIpAddrToLong >  startIpAddrToLong) {
            console.log('success');
          $("#validate").val("True");
        }
       
    });
    
    function IpAddressToLong(ip){
            return ip.split('.').map((octet, index, array) => {
        return parseInt(octet) * Math.pow(256, (array.length - index - 1));
            }).reduce((prev, curr) => {
        return prev + curr;
        });

}

working sample http://jsfiddle.net/tjmcdevitt/mdkx9h0w/31/

Something like this is what I am trying to do. enter image description here

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda