Validation via Javascript does not work

1

Person, beauty? So, I have an alert div (that of the bootstrap) with the display none and visibility hidden I'm catching it by id, both in css and javascript, but it does not work. It changes the properties of the CSS but still sends the data to PHP, the validation is not worth anything haha. Help me please. this is one of the inputs I want to validate.

    <input type="text" name="pergunta" data-placement="bottom" class="form-control" placeholder="Ex: XXXXXXXXXX?" aria-describedby="basic-addon1">

This is the div that I put as hidden by default, it is positioned below the input:

<div class="alert alert-danger" id="valida_pergunta" role="alert"><span class="glyphicon glyphicon-info-sign"></span> Preencha o campo Pergunta</div>

When I click, I want it to display the div that looks like hidden and does not send anything to PHP.

  function validar (){
var pergunta = form_pergunta.pergunta.value;
var resposta = form_pergunta.resposta.value;
var desafio = form_pergunta.desafio.value;
if(pergunta==""){
  document.getElementById("valida_pergunta").style.display = "flex";
}
if (resposta == "") {
  document.getElementById("valida_resposta").style.display ="flex";
}
if (desafio ==""){
  document.getElementById("valida_desafio").style.display = "flex";
}
      }              
    
asked by anonymous 07.01.2017 / 01:38

1 answer

1

Take a look at your code, should have something else affected this behavior.

Another thing, there is no need to change visibility and display. Only the display is sufficient.

Look at this code that I did for example.

Only the display has already worked.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><linkhref="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script><inputtype="text" id="input" />
<div class="alert alert-danger" id="valida_pergunta" role="alert" style="display: none;"><span class="glyphicon glyphicon-info-sign"></span> Preencha o campo Pergunta</div>
<button onclick="validar()">VAI</button>

<script>
  function validar() {
    var input = document.getElementById("input").value;

    if (input == "") {
      document.getElementById("valida_pergunta").style.display = "inline";
    }
  }
</script>

UPDATE

As chatted in chat, it was identified that there is no action preventing the submit.

To solve this, let's follow best practices and run all of our JS when the DOM is ready.

For this:

window.onload = function(){
      //Código aqui
}

First thing to do is to have access to the button and the form, so we'll create two variables:

var button = document.getElementById("enviar");
var form = document.getElementById("form_pergunta");

(Do not forget to put the id="send" it in the HTML button)

With access to this, we can perform a function every time the button is clicked, for this we will use the addEventListener() on the button and put the following logic: CASE THE VALIDATE FUNCTION RETURNS TRUE, GIVE A SUBMIT NO FORM .

button.addEventListener("click", function(e){
    if(validar(form)) form.submit();
});

Now let's look at the validate function. This will get a parameter that is the very form we retrieved above.

function validar (form_pergunta){ 
 var pergunta = form_pergunta.pergunta.value; 
 var resposta = form_pergunta.resposta.value; 
 var desafio = form_pergunta.desafio.value; 
 var passou = true;
 if(pergunta==""){ 
  document.getElementById("valida_pergunta").style.display = "flex"; 
   passou = false;
 } 
 if (resposta == "") { 
  document.getElementById("valida_resposta").style.display ="flex"; 
  passou = false;
} 
if (desafio ==""){ 
  document.getElementById("valida_desafio").style.display = "flex"; 
  passou = false;
}
return passou;    

}

That way it should work.

I hope I have helped.

    
07.01.2017 / 02:00