Jump to content
Fórum Script Brasil
  • 0

[Resolvido] - Javascript para ao pular id sequencial


dinohills

Question

Boa tarde a todos,

estou criando uma validação em javascript para não deixar que a pessoa digite ip duplicado ou que o ip digitado saia fora do padrão que eu defini, que no caso é (192.168.1.xxx).

Até ai está tudo funcionando!!

Na minha tabela eu coloquei opção para inserir e deletar as linhas, e para cada linha inserida cada input recebe uma id que no caso são:

ip-x

mac-x

nome-x

O problema é quando eu mando excluir uma linha que está entre 2 números ou seja:

em uma tabela com 5 linhas se eu mandar excluir a linha 4 a minha verificação para na linha 3 deixando a linha 5 de fora.

Aqui está um exemplo real do meu problema:

<html>
    <head>
        <title></title>
        &lt;script type="text/javascript">
            function adiciona(){            
                var tbl = document.getElementById("tabela");             
                var novaLinha = tbl.insertRow(-1);
                var novaCelula;
                var id = tbl.rows.length -2; 
                            
                novaCelula = novaLinha.insertCell(0);                 
                novaCelula.innerHTML = "\n<input type='text' name='IP[]' id='ip-"+id+"' maxlength='13' size='15' onkeydown='mascara(this,ip)' onkeyup='mascara(this,ip)' />";
                             
                novaCelula = novaLinha.insertCell(1);                        
                novaCelula.innerHTML = "\n<input type='text' name='MAC[]' id='mac-"+id+"' maxlength='17' size='15' />";
                            
                novaCelula = novaLinha.insertCell(2);                       
                novaCelula.innerHTML = "\n<input type='text' name='NOME[]' id='nome-"+id+"' maxlength='25' size='20' />";
                
                novaCelula = novaLinha.insertCell(3);                        
                novaCelula.innerHTML = "\n<input type='button' value='Deletar' onclick='deleteRow(this.parentNode.parentNode.rowIndex)'>";
            }
            function deleteRow(valor){
                document.getElementById('tabela').deleteRow(valor)
            }
            function valida_tudo(){
                var padrão = /^(192)\.(168)\.(1)\./;
                var padrao2 = /^(\d{1,3})\.(\d{1,3})\.(\d{1})\.(\d{1,3})/;                
                var nome_ip = document.getElementsByName("IP[]");                
                
                for (i = 0; i < nome_ip.length; i++){
                    var id_ip = document.getElementById("ip-"+i);
                    var ip_array = id_ip.value.match(padrao2); 
                    
                    if ( padrão.test(id_ip.value) == false || ip_array[4] > 254 || ip_array[4] == 0){
                        alert("Este ip: "+id_ip.value+" é inválido !!");
                        return false;
                    }                    
                    for (n = 0; n < i; n++){
                        var id_ip2 = document.getElementById("ip-"+n);
                        if (id_ip.value == id_ip2.value){
                            alert("Ip's iguais não são permitidos !!");
                            return false;
                        }
                    }
                }
            }
            function mascara(el,masc){
                el.value=masc(el.value);
            }                      
            function ip(d){
                d=d.replace(/\D/g,""); //permite somente numeros
                d=d.replace (/\s/g,""); // remove espaço 
                d=d.replace(/^(\d{3})(\d)/,"$1.$2");
                d=d.replace(/^(\d{3})\.(\d{3})(\d)/,"$1.$2.$3");
                d=d.replace(/^(\d{3})\.(\d{3})\.(\d{1})(\d)/,"$1.$2.$3.$4");
                return d;
            }                
        </script>
    </head>
    <body>
        <form action="" method="post" >

            <table border="1" cellpadding="5" cellspacing="0" id="tabela">
                <thead>
                    <tr>
                        <th>IP</th>
                        <th>MAC ADDRESS</th>
                        <th>Nome</th>                        
                    </tr>
                </thead>

                <tbody>
                    <tr>
                       <td><input type=text name="IP[]"   id="ip-0"   size="15"  maxlength="13" onkeydown="mascara(this, ip)" onkeyup="mascara(this, ip)" value="192.168.1.1" /></td>
                        <td><input type=text name="MAC[]"  id="mac-0"  size="15"  maxlength="17" value="11-11-11-11-11-11" /></td>
                        <td><input type=text name="NOME[]" id="nome-0" size="20"  maxlength="25" value="PC1" /></td>
                        <td><input type="button" value="Deletar" id="0" onclick="deleteRow(this.parentNode.parentNode.rowIndex)" /></td>
                    </tr>
                    <tr>
                       <td><input type=text name="IP[]"   id="ip-1"   size="15"  maxlength="13" onkeydown="mascara(this, ip)" onkeyup="mascara(this, ip)" value="192.168.1.2" /></td>
                        <td><input type=text name="MAC[]"  id="mac-1"  size="15"  maxlength="17" value="22-22-22-22-22-22" /></td>
                        <td><input type=text name="NOME[]" id="nome-1" size="20"  maxlength="25" value="PC2" /></td>
                        <td><input type="button" value="Deletar" id="1" onclick="deleteRow(this.parentNode.parentNode.rowIndex)" /></td>
                    </tr>
                    <tr>
                       <td><input type=text name="IP[]"   id="ip-2"   size="15"  maxlength="13" onkeydown="mascara(this, ip)" onkeyup="mascara(this, ip)" value="192.168.1.3" /></td>
                        <td><input type=text name="MAC[]"  id="mac-2"  size="15"  maxlength="17" value="33-33-33-33-33-33" /></td>
                        <td><input type=text name="NOME[]" id="nome-2" size="20"  maxlength="25" value="PC3" /></td>
                        <td><input type="button" value="Deletar" id="2" onclick="deleteRow(this.parentNode.parentNode.rowIndex)" /></td>
                    </tr>
                    <tr>
                       <td><input type=text name="IP[]"   id="ip-3"   size="15"  maxlength="13" onkeydown="mascara(this, ip)" onkeyup="mascara(this, ip)" value="192.168.1.4" /></td>
                        <td><input type=text name="MAC[]"  id="mac-3"  size="15"  maxlength="17" value="44-44-44-44-44-44" /></td>
                        <td><input type=text name="NOME[]" id="nome-3" size="20"  maxlength="25" value="PC4" /></td>
                        <td><input type="button" value="Deletar" id="3" onclick="deleteRow(this.parentNode.parentNode.rowIndex)" /></td>
                    </tr>                    
                </tbody>
            </table>            
            <input type="button" value="Inserir" onclick="adiciona();" >
            <input type="button" value="Validar" onclick="valida_tudo();" >
        </form>
    </body>
</html>

Gostaria de saber se tem algum jeito de fazer o script continuar mesmo se uma id está faltando.

Se alguém tiver uma dica ou souber de algo poste ai por favor.

Obrigado!!

Edited by dinohills
Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Solução encontrada, ao invés de usar a id usei a name !!

function valida_tudo(){
                var padrão = /^(192)\.(168)\.(1)\./;
                var padrao2 = /^(\d{1,3})\.(\d{1,3})\.(\d{1})\.(\d{1,3})/;                
                var nome_ip = document.getElementsByName("IP[]");                
                var nome_ip2 = document.getElementsByName("IP[]");                
                
                for (i = 0; i < nome_ip.length; i++){                                        
                    var ip_array = nome_ip[i].value.match(padrao2); 
                    
                    if ( padrão.test(nome_ip[i].value) == false || ip_array[4] > 254 || ip_array[4] == 0 ){
                        alert("Este ip: "+nome_ip[i].value+" é inválido !!");
                        return false;
                    }                    
                    for (n = 0; n < i; n++){                        
                        if (nome_ip[i].value == nome_ip2[n].value){
                            alert("Ip's iguais não são permitidos !!");
                            return false;
                        }
                    }
                }
            }

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...