Form validation with jquery

Asked

Viewed 1,358 times

0

I have to do a form validation for a college job, but it’s not working and I’m not succeeding in finding the error. Grateful for the help.

<script>

    $(document).ready( function(){ //Quando documento estiver pronto
    $("#tel").mask("(00) 0000-00009");
    $('#btn').click( function(){ /* Quando clicar em #btn */
    /* Coletando dados */
    var nome  = $('#nome').val(); 
    var email = $('#email').val(); 
    var tel = $('#tel').val();  
    var msg  = $('#msg').val();  


    if(nome.indexOf(" ") == -1){
       $("#nome").html('Nome invalido') 
    }
    if (nome.length < 10){
       $("#nome").html('Nome invalido') 
    }     
    if(email != ""){
      var filtro = /^([w-]+(?:.[w-]+)*)@((?:[w-]+.)*w[w-]{0,66}).([a-z]{2,6}(?:.[a-z]{2})?)$/i;
    if(filtro.test(email)){
      return true;
    }else {
       $("#email").html('O endereço de email fornecido é invalido')
       return false;
     }
      else{
        $("#email").html('Forneça um endereço de email')
        return false;
        }
      };

    if(msg.length < 1){           
        $("#msg").html(' Digite a mensagem')                    
        return false;           
    }                  
  • What error appears? Be more specific if possible :)

  • So it just doesn’t work. It doesn’t show the messages, so I’m confused because I think it’s right.

  • There are a lot of errors visible in this code that you sent rs,when you click on F12 and displays the console, which error message you receive?

1 answer

2


There are several small errors in the code you sent, but can not fix exactly the way you should have imagined without more information how it should work.

$(document).ready(function() {
  $("#tel").mask("(00) 0000-00009");
  $('#btn').click(function() {
    var nome = $('#nome').val();
    var email = $('#email').val();
    var tel = $('#tel').val();
    var msg = $('#msg').val();
    if (nome.indexOf(" ") == -1) {
      $("#erros").html('Nome invalido');
      return false;
    }
    if (nome.length < 10) {
      $("#erros").html('Nome invalido');
      return false;
    }
    if (email != "") {
      var filtro = /^([w-]+(?:.[w-]+)*)@((?:[w-]+.)*w[w-]{0,66}).([a-z]{2,6}(?:.[a-z]{2})?)$/i;
      if (filtro.test(email)) {
        return true;
      } else {
        $("#erros").html('O endereço de email fornecido é invalido');
        return false;
      }
    } else {
      $("#erros").html('Forneça um endereço de email');
      return false;
    }
    if (msg.length < 1) {
      $("#erros").html(' Digite a mensagem');
      return false;
    }
  });


});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://igorescobar.github.io/jQuery-Mask-Plugin/js/jquery.mask.min.js"></script>
Nome:
<input type="text" id="nome" name="nome">
<br/>Email:
<input type="text" id="email" name="email">
<br/>Telefone:
<input type="text" id="tel" name="tel">
<br/>Msg:
<input type="text" id="msg" name="msg">
<br/>
<span id="erros" name="erros"></span>
<br/>
<input type="button" id="btn" name="btn" value="validar">

Browser other questions tagged

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