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
Check for Empty Inputs and Enforce Users to Fill out Fields

I have a question about JQuery and While statements. I am pretty new to coding and one of my tasks is to check if there are any empty inputs when a user tries to submit a value, and return an alert. I first started by using if statements, and saw that only the first if statement would be read when I clicked on the submission button. I tried to implement a while loop to continuously loop through all of the statements until all sections were filled out, but I am not sure if I am going about this the right way.

I am not sure what to put in the while loop because I know it should be true, but true to some value. Any help would be appreciated!

let assetInfo = {
            asset_tag_no: $(`#asset_tag_no${i}`).val(),
            manufacturer: $(`#manufacturer_serial_no${i}`).val(),
            descriptions: $(`#description${i}`).val(),
            costs: $(`#cost${i}`).val(),
            po_no: $(`#p.o._no${i}`).val(),
            department_division_head: $(`#department_division_head${i}`).val(),
        }

        // Check for Empty Inputs and Enforce Users to Fill out Fields
        
        
        $('#submit').click(function(){
            while (assetInfo = true) {

                if (assetInfo.asset_tag_no == '') continue;{
                    alert('Asset Tag Number can not be left blank');
                    
                }
                if (manufacturer == '') continue; {  
                    alert('Manufacturer Serial Number can not be left blank');
                    
                }
                if (descriptions == '') continue;{  
                    alert('The Description can not be left blank');
                }
                if (costs == '') continue;{  
                    alert('The Cost can not be left blank');
                }  
                if (po_no == '') continue;{  
                    alert('The P.O Number/ Document Number can not be left blank');
                }   
                if (department_division_head == '') continue;{  
                    alert('The Remarks can not be left blank');
                }
                else{
                    alert('Submission Accepted');
                    break;
                    }
            
            }
            return assetInfo;   
            });
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try this technique man,

let assetInfo = {
            asset_tag_no: {
        label: "Asset Tag",
            value: $(`#asset_tag_no${i}`).val()
            },
  manufacturer: {
        label: "Manufacturer",
            value: $(`#manufacturer_serial_no${i}`).val()
            },
           
        }

        // Check for Empty Inputs and Enforce Users to Fill out Fields
        
        let keys = Object.keys(assetInfo);
        
        for(let i = 0 ; i < keys.length ; i++){
            if(assetInfo[keys[i]].value === "")
          {
                alert(assetInfo[keys[i]].label+' can not be left blank');
          }
                          
        }
about 4 years ago · Juan Pablo Isaza Relatório

0

You can remove the while loop and just do it with a for loop. First of all you should loop over the assetInfo object and check if all the values match your needs. Throw alerts when needed.

for (const info in assetInfo) {
  if (assetInfo[info] == "") {
    alert(`${info} shouldn't be blank!`);
  }
}
about 4 years ago · Juan Pablo Isaza Relatório
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