How to validate HTML form with Javascript?

0

I'm trying to do a function in javascript to validate a form, I want it the function scroll through the form and check which fields are required (required) and then add those fields to a list and then manipulate and handle the errors:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">

    <!--JQUERY AND BOOSTRAP-->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><linkrel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

    <!--JS-->
    <script src="function.js"></script>

</head>
<body>

    <form id="formTeste">
        <div class="col-sm-4">
            <label for="textoObrigatorio">Texto Obrigatorio</label>
            <input type="text" id="textoObrigatorio" required />
        </div>
        <button type="button" onclick="ValidarFormulario('formTeste')">Validar</button>
    </form>

</body>
</html>

Javascript

function ValidarFormulario(idForm) {
    var formulario = document.getElementById(idForm);
    //PERCORRER O FORMULARIO
    //VERIFICAR OS ELEMENTOS OBRIGATORIOS
}

The difficulty is where to find input elements within the form?

    
asked by anonymous 04.07.2018 / 18:46

1 answer

2

function ValidarFormulario(id){
  const idForm = document.getElementById(id).getAttribute('id');
  document.querySelectorAll('#' + idForm + ' input').forEach(function(a){
      if(a.value.length < 1 && a.hasAttribute('required')) {
          console.log('Por favor, preencha todos os campos!');
      }
  });
}
<html>
<head>
    <meta charset="utf-8">

    <!--JQUERY AND BOOSTRAP-->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><linkrel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

    <!--JS-->
    <script src="function.js"></script>

</head>
<body>

    <form id="formTeste">
        <div class="col-sm-4">
            <label for="textoObrigatorio">Texto Obrigatorio</label>
            <input type="text" id="textoObrigatorio" required placeholder="precisa preencher" />
            <input type="text" id="textoObrigatorio2" placeholder="nao precisa preencher" />
        </div>
        <button type="button" onclick="ValidarFormulario('formTeste')">Validar</button>
    </form>

</body>
</html>
    
04.07.2018 / 19:09