0

There is my code i check a simple formulmary and i test with a function if my label "prenom" is empty or not. But when i test it's ok but the page is refreshing and so everything you wrote is destruct. How can i stop the process until the script is write ? thank you

<!--
/** * Formulaire classique */
-->

<html lang="fr">

<head>
  <title>Formulaire</title>
  <meta charset="utf-8">
  <link href="style_form.css" rel="stylesheet" media="all" type="text/css">
</head>

<body>
  <h1>Votre avis</h1>
  <form id="form" method="post" enctype="text/plain">
    <p>
      <label for="prenom">Prénom</label><input id="prenom" type="text" name="prenom" value=" ">
      <label for="nom">Nom</label><input id="nom" type="text" name="nom" value=" "><br>

      <input id="sexeHomme" type="radio" name="sexe" value="homme"><label for="sexeHomme">Homme</label>
      <input id="sexeFemme" type="radio" name="sexe" value="femme"><label for="sexeFemme">Femme</label><br>

      <textarea name="impression" rows="5" cols="50">Donnez vos impressions sur le site...</textarea><br>

      <button onclick="myFunction()">Envoyer</button>

      <script>
        function myFunction() {
          var prenom;

          if (prenom.trim() == "") {
            console.log("Le labbel est vide");
          }
        }
      </script>

    </p>
  </form>

</body>

</html>

/** here is the probleme

Esko
  • 4,109
  • 2
  • 22
  • 37
  • 3
    Possible duplicate of [JavaScript code to stop form submission](https://stackoverflow.com/questions/8664486/javascript-code-to-stop-form-submission) – Esko Oct 10 '18 at 09:05
  • add event.preventDefault(); to your javascript, this prevents the form from submitting (prevents the default behaviour of an element interaction) if you want to run javascript validation on the form you can use document.getElementById("form").submit(); to submit the form when the criteria is met – digital-pollution Oct 10 '18 at 09:09

2 Answers2

1

From your script you can do this:

<!--
/** * Formulaire classique */
-->

<html lang="fr">

<head>
  <title>Formulaire</title>
  <meta charset="utf-8">
  <link href="style_form.css" rel="stylesheet" media="all" type="text/css">
</head>

<body>
  <h1>Votre avis</h1>
  <form id="form" method="post" enctype="text/plain">
    <p>
      <label for="prenom">Prénom</label><input id="prenom" type="text" name="prenom" value=" ">
      <label for="nom">Nom</label><input id="nom" type="text" name="nom" value=" "><br>

      <input id="sexeHomme" type="radio" name="sexe" value="homme"><label for="sexeHomme">Homme</label>
      <input id="sexeFemme" type="radio" name="sexe" value="femme"><label for="sexeFemme">Femme</label><br>

      <textarea name="impression" rows="5" cols="50">Donnez vos impressions sur le site...</textarea><br>

      <button type="submit">Envoyer</button>

      <script>
      var form=document.getElementById('form');
      form.addEventListener('submit',function(event)
      {
      event.preventDefault();
          var prenom;
           prenom=document.getElementById('prenom').value;
          if (prenom.trim() == "") {
            console.log("Le labbel est vide");
        }
      });
       
      </script>

    </p>
  </form>

</body>

</html>
Gratien Asimbahwe
  • 1,606
  • 4
  • 19
  • 30
0

you need to call the function in form like

<form id="form" method="post" enctype="text/plain" onsubmit="return myFunction() ">


<script>
        function myFunction() {
          var prenom=document.getElementById('prenom').value;

          if (prenom== "") {
            console.log("Le labbel est vide");
return false
          }
        }
      </script>
JIJOMON K.A
  • 1,290
  • 3
  • 12
  • 29