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

159
Visualizações
¿Por qué no se muestra la condición falsa para checkValidity()?

Cuando el campo de búsqueda está vacío, el mensaje de error debería decir 'entrada vacía', pero ¿no es así? No entiendo por qué no funciona. En su lugar, muestra la información sobre herramientas predeterminada del navegador que se muestra en un campo obligatorio.

Retazo

 const searchForm = document.querySelector('#search-form'); const searchFormSubmitted = (e) => { e.preventDefault(); const inpObj = document.getElementById("id1"); if (inpObj.checkValidity()) { document.getElementById("error-message").innerHTML = "input is ok"; } else { document.getElementById("error-message").innerHTML = "input empty"; } } searchForm.addEventListener('submit', searchFormSubmitted);
 <form id="search-form" class="search-diets-form"> <input id="id1" type="text" value="" placeholder="Search.." name="search" required> <select name="healthList" id="healthList"> <option selected="selected" class="health-option">Health options</option> <option class="health-option" value="dairy-free"> Dairy free </option> <option class="health-option" value="alcohol-free"> Alcohol free </option> <option class="health-option" value="egg-free"> Egg free </option> <option class="health-option" value="fish-free"> Fish free</option> <option class="health-option" value="low-sugar"> Low sugar </option> <option class="health-option" value="vegetarian"> Vegetarian </option> </select> <select name="caloriesList" id="caloriesList"> <option selected="selected">Max number of calories</option> <option value="300"> 300 </option> <option value="400"> 400 </option> <option value="500"> 500 </option> <option value="600"> 600 </option> <option value="700"> 700 </option> <option value="800"> 800 </option> </select> <button type="submit">Search recipes</button> </form> <b id="error-message"> </b>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Debe agregar el atributo novalidate en la etiqueta del form

Nota: también mejoré su condición usando un operador ternario

 const searchForm = document.querySelector('#search-form'); const inpObj = document.getElementById("id1"); const errorMsg = document.getElementById("error-message") const searchFormSubmitted = e => { e.preventDefault(); errorMsg.innerHTML = inpObj.checkValidity() ? "input is ok" : "input empty"; } searchForm.addEventListener('submit', searchFormSubmitted);
 <form id="search-form" class="search-diets-form" novalidate> <input id="id1" type="text" value="" placeholder="Search.." name="search" required> <select name="healthList" id="healthList"> <option selected="selected" class="health-option">Health options</option> <option class="health-option" value="dairy-free"> Dairy free </option> <option class="health-option" value="alcohol-free"> Alcohol free </option> <option class="health-option" value="egg-free"> Egg free </option> <option class="health-option" value="fish-free"> Fish free</option> <option class="health-option" value="low-sugar"> Low sugar </option> <option class="health-option" value="vegetarian"> Vegetarian </option> </select> <select name="caloriesList" id="caloriesList"> <option selected="selected">Max number of calories</option> <option value="300"> 300 </option> <option value="400"> 400 </option> <option value="500"> 500 </option> <option value="600"> 600 </option> <option value="700"> 700 </option> <option value="800"> 800 </option> </select> <button type="submit">Search recipes</button> </form> <strong id="error-message"> </strong>

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