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

203
Visualizações
How to write a js script that verifies input on html

I am trying to add validation to a html form that looks like this:

<div id="container">
  <header>
    <h1>Production Form</h1>
  </header>
  </br>
  <div class="content">
    <div class="row">
      <article class="col-xs-12">
        <form id="cf-task-form">
          <section>                   
            <br><br>
                <div class="row form-group">
                  <div class="col-xs-3">
                    <label for="link">Link</label>
                    <input type="url" id="link" class="form-control" name="output[link]">
                  </div>
                  <div class="col-xs-3">
                    <label for="address">Address</label>
                    <input id="address" class="form-control" name="output[address]">
                  </div>
                  <div class="col-xs-3">
                    <label for="research">Research</label>
                    <select id="research" name="output[research]">
                      <option value="0">0</option>
                      <option value="1">1</option>                              
                    </select>                            
                  
                </div>
          </section>

          <div class="col-xs-offset-6 col-xs-6">
            <input type="button" class="btn sub-btn pull-right" id="submit-btn" value="Submit" tabindex="10">
          </div>
        </form>
      </article>
    </div>
      <div class="clear"></div>
  </div>
</div>

However the code is not picking validation when I use the required attribute, how can I add validation for this within a js script that will check against the values in this html form before submitting

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

0

I think maybe you get the errors because the HTML tags were not opened and closed in the correct order, and you had an opened div that needed to be closed. Try validating the HTML code. I think it can cause problems if your html is not valid.

I added these attributes to your HTML (and validated it), and also some CSS to see the validation result.

<input type="text" id="link" class="form-control" name="output[link]" required="true" pattern="^http.*">
<input id="address" class="form-control" name="output[address]" required="true" pattern=".*street.*">

and

:invalid {
  border: 1px solid red;
}

This works and validates as expected.

:invalid {
  border: 1px solid red;
}

input {
  margin: 1em
}
<div id="container">
  <header>
    <h1>Production Form</h1>
  </header>
  <div class="content">
    <div class="row">
      <article class="col-xs-12">
        <form id="cf-task-form">
          <section>
            <div class="row form-group">
              <div class="col-xs-3">
                <label for="link">Link</label>
                <input type="text" id="link" class="form-control" name="output[link]" required="true" pattern="^http.*">
              </div>
              <div class="col-xs-3">
                <label for="address">Address</label>
                <input id="address" class="form-control" name="output[address]" required="true" pattern=".*street.*">
              </div>
              <div class="col-xs-3">
                <label for="research">Research</label>
                <select id="research" name="output[research]">
                  <option value="0">0</option>
                  <option value="1">1</option>
                </select>

              </div>


              <div class="col-xs-offset-6 col-xs-6">
                <input type="button" class="btn sub-btn pull-right" id="submit-btn" value="Submit" tabindex="10">
              </div>
            </div>
          </section>
        </form>
      </article>
    </div>
    <div class="clear"></div>
  </div>
</div>

Note that the validation patterns are only for testing and not real patterns that should be used!

I used this page as reference: https://developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation

about 4 years ago · Juan Pablo Isaza Relatório

0

If you want the required attribute to work, I think you need to change your last input type to "submit" :

<input type="submit" class="btn sub-btn pull-right" id="submit-btn" value="Submit" tabindex="10">

But I agree that it would be great to add some JS if you want to do proper form validation. I see some people shared useful links on the subject.

about 4 years ago · Juan Pablo Isaza Relatório

0

<form id="cf-task-form" onsubmit="return validateForm()">    
const link = document.querySelector("#link").value
const Address = document.querySelector("#Address").value
const research = document.querySelector("#research").value
function validateForm() {
  if (link == '') {
    alert("filled out url");
    return false;
  }
  else if (Address == '') {
    alert("filled out Address");
    return false;
  }
  else if (research == '') {
    alert("filled out research");
    return false;
  }
}
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