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

115
Visualizações
Restrict the input field value of a form using input.validity.valid function

I am trying to select a input value from the selected list.Input value (stationerytype) containing spaces or braket are not getting selected,What is wrong with my code For example stationery type CELLOTAPE(BROWN) is not getting selected whereas other value are getting selected.

function random() {
  const input = document.querySelector('[name="stationerytype[]"]');
  input.value = ""
  var a = document.getElementById('purpose').value;
  if (a === "Meeting") {
    var datalist = "datalist1";
  } else if (a === "Departmental") {
    var datalist = "datalist2";
  }
  const options = Array.from(document.getElementById(datalist).options).map(option => option.value);
  input.setAttribute("list", datalist);
  input.setAttribute("pattern", options.join('|'));
}
function ondataListSelect() {
  const input = document.getElementById('stationerytype');
  if (!input.validity.valid) {
    input.value = '';
  }
}
<select type="text" name="purpose" id="purpose" class="form-control" onchange="random()" required >
  <option></option>
  <option value="Meeting">Meeting</option>
  <option value="Departmental">Departmental</option>
</select>
  
<td>
  <input type="text"
    name="stationerytype[]"
    id="stationerytype"
    class="form-control"
    onchange="ondataListSelect()"
    autocomplete="off"
    required>
  <datalist id="datalist1">
    <option value=""></option>
    <option value="MEETING PEN">MEETING PEN</option>
    <option value="NOTEPAD">NOTEPAD</option>
    <option value="PLASTIC FOLDER">PLASTIC FOLDER</option>
  </datalist>

  <datalist id="datalist2">
    <option value=""></option>
    <option value="A4 GREEN REAM">A4 GREEN REAM</option>
    <option value="A4 WHITE REAM">A4 WHITE REAM</option>
    <option value="CELLOTAPE(BROWN)">CELLOTAPE(BROWN)</option>
  </datalist>
</td>

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

0

This is because ( and ) have special meaning in regex, but you concatenate all those values directly with a | in between. You need to properly escape any special characters in the values, that are not meant to have their special meaning.

Using the solution from the accepted answer to Is there a RegExp.escape function in JavaScript?, you can fix the issue by changing the following from

const options = Array.from(document.getElementById(datalist).options)
                .map(option => option.value);

to

const options = Array.from(document.getElementById(datalist).options)
                .map(option => option.value.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'));
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