1

the validation is working on the input fields, but it resists to do so on the textarea. Why? When i log the length of the textarea to the console, it says that document.guest is undefined... but in my opinion this is obviously not the case, because its working on the input fields.

  <script type="text/javascript">
  function fcheck() {
    if (document.guest.name.value.length < 1) {
      alert("Du musst einen Namen eingeben!");
      return false;
    }
    if (document.guest.email.value.length < 1) {
      alert("Du musst eine Email eingeben!");
      return false;
    }
    if (document.guest.message.value.length < 1) {
      alert("Du musst eine Nachricht eingeben!");
      return false;
    }
    return true;
  }
  </script>
<div id="test2" class="col-md-6 form-group">
      <form action="addcomment.php" method="post" name="guest" onSubmit="return fcheck();">

        <div class="textform">
            <p>Name:
            <input type="text" name="name" class="form-control"/>
            </p>
        </div>

        <div class="textform">
            <p>E-Mail:
            <input type="text" name="email" class="form-control"/>
            </p>
        </div>

        <div class="textform">
            <p>Nachricht(Maximal 300 Zeichen):</p>
            <textarea cols="50" name="message" rows="10" class="form-control status-box" maxlength="300"> </textarea>
        </div>
        <br>
        <div class="pull-left">
            <p class="counter">300</p>
        </div>

        <div id="textarea" class="pull-right">
            <input type="submit" value="Nachricht abschicken" class="btn btn-primary" />
        </div>
      </form>
    </div>
Pingbeat
  • 305
  • 1
  • 9

3 Answers3

2

First of all, you need to prevent form default action using preventDefault. So replace this line of code in html: (for more info : JavaScript code to stop form submission )

onSubmit="return fcheck();"

with :

onSubmit="event.preventDefault(); fcheck();"

second, in your javascript:

<script type="text/javascript">
    function fcheck() {
        if (document.forms.guest.name.value.trim().length < 1) {
            alert("Du musst einen Namen eingeben!");
            return false;
        }
        if (document.forms.guest.email.value.trim().length < 1) {
            alert("Du musst eine Email eingeben!");
            return false;
        }
        if (document.forms.guest.message.value.trim().length < 1) {
            alert("Du musst eine Nachricht eingeben!");
            return false;
        }
        return true;
    }
</script>
Community
  • 1
  • 1
Mehran Torki
  • 977
  • 1
  • 9
  • 37
1

You should write :

document.forms.guest

Instead of :

document.guest

You should also have a peek on this answer.

EDIT : in fact, your textarea has a length of "1" ! See this edited fiddle. It's due to the space character. Remove it and it will works.

Community
  • 1
  • 1
Amessihel
  • 5,891
  • 3
  • 16
  • 40
1

textareas dont show on document.form, you can access it like so:-

document.guest.elements["message"].value
BenG
  • 14,826
  • 5
  • 45
  • 60