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

322
Visualizações
select2 required oninvalid custom text not working

Here i have select option using select2, it works fine, but here i want select2 required with oninvalid custom text and when i select and save with button form, but it keeps displaying invalid text on button and can't save

This works if we directly select select2 and fill in the input and directly save

But it doesn't work when I press the button in the beginning and select the last one select2, it will keep displaying the text and that's the problem is there any way to fix it

$(".select").select2({
 placeholder: "Pilih . . .",
 allowClear: false,
 });
.w-100{
  width: 50%;
}
.btn{
  width: 100px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.7/css/select2.min.css" rel="stylesheet">

<h4>FORM 1</h4>
<form action="ins.php" method="POST" >
  <select  class="select w-100" oninvalid="this.setCustomValidity('Tidak Boleh Kosong')" oninput="this.setCustomValidity('')"  required="" >
      <option value=""></option>
      <option value="1">Opsi 1</option>
      <option value="2">Opsi 2</option>
      <option value="3">Opsi 3</option>
      <option value="4">Opsi 4</option>
      <option value="5">Opsi 5</option>
  </select>
  <br><br>
  <input  type="text" class="w-100"  oninvalid="this.setCustomValidity('Tidak Boleh Kosong')" oninput="this.setCustomValidity('')"  value="" required="" />
  <br><br>
  <button class="btn btn-success">Simpan</button>
 </form>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.7/js/select2.min.js"></script>

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

0

The oninput event is only valid for input / textarea elements. For select elements onchange should be used instead. The onchange event occurs when the element looses focus and has a changed value.

Updating the select to use onchange as follows will correctly trigger the event.

  <select  class="select w-100" oninvalid="this.setCustomValidity('Tidak Boleh Kosong')" onchange="this.setCustomValidity('')"  required="" >
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