JavaScript code to prevent form submission

I read a lot of articles at web about this. I tried to fit all this instruction in my code but i have the same problem. The code of form is below:

<form action="#" method="post" id="commentform" onsubmit="return liontas();" >
              <p>
                <input type="text" name="author" id="author" value="" size="22" tabindex="1" />
                <label for="author"><small>Όνοματεπώνυμο (υποχρεωτικό)</small></label>
              </p>
              <p>
                <input type="text" name="email" id="email" value="" size="22" tabindex="2" />
                <label for="email"><small>Ε-Mail (ΔΕΝ θα δημοσιευθεί) (υποχρεωτικό)</small></label>
              </p>
              <p>
                <input type="text" name="url" id="url" value="" size="22" tabindex="3" />
                <label for="url"><small>Τηλέφωνο επικοινωνίας (υποχρεωτικό)</small></label>
              </p>
              <p>
                <input type="text" name="subject" id="subject" value="" size="22" tabindex="3" />
                <label for="subject"><small>Θέμα-Πρόβλημα-Αίτηση (υποχρεωτικό)</small></label>
              </p>
              <p>
                <textarea name="comment" id="comment" cols="100%" rows="10" tabindex="4"></textarea>
              </p>
              <p>
                  <button name="submit"  type="submit" id="submit" >Submit Comment</button>
              </p>
            </form>

The script that i have the liontas() is below:

<script>
   function liontas(){
   $('document').ready(function (){
      var name=$('#author').val();
      var userEmail=$('#email').val();
      var phone=$('#url').val();
      var subject=$('#subject').val();
      var comment=$('#comment').val();
      var form={
          "name":name,
          "userEmail":userEmail,
          "phone":phone,
          "subject":subject,
          "comment":comment
      };
    console.log(form);
    var array="Συμπληρώστε τα πεδία: ";
    //console.log(array.length);
    for (var item in form) {
        console.log(form[item]);
        if(form[item]==""){
            array=array+"\n"+"\n"+item+"\n";
        }
    }
    if(array.length>22){
    console.log("false");    
    alert(array);
    
    return false;
    }
   });
   };
   
</script>

It seems that the liontas() is not return false for some reason. What is going wrong?How i can fix it?

let’s fold the code a bit and see

function liontas(){
  $('document').ready(function (){ /* ... */ });
}
1 Like

???Your answers are like Pythia’s instructions…:slight_smile:
“Tell to the king that the carven hall is fallen in decay;
Apollo has no chapel left, no prophesying bay,
No talking spring. The stream is dry that had so much to say.”

You are targeting an element called ‘document’? Check your syntax for document.ready(function())

1 Like

I throw out the $('document').ready(function (){ and it works!!!. But I can’t understand why this function caused the problem.

Thanks a lot.

The document shouldn’t have quotes around it, I think.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.