How to validate an HTML form with Javascript?

Asked

Viewed 904 times

0

I’m trying to do a javascript function to validate a form, I want that function scroll through the form and check which fields are required (required) and then add these fields into a list to 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>
    <link rel="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 inside the form?

1 answer

2


The required will already do this for you, will not let you submit without filling in the input, but with JS you can check if the value is empty:

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>
    <link rel="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>

  • Okay, it worked, thank you so much for your help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.