Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[Javascript] Formular Validierung

Empfohlene Antworten

Veröffentlicht

Hi!

Bin in Javascript nicht so fit. Ich versuche ein Formular zu validieren, sprich wenn ein Pflichtfeld nicht ausgefüllt ist, soll ne Messagebox, die rummeckert, erscheinen.

Hier mal (stark vereinfacht) was ich bis jetzt gemacht hab:

Javascript (check.js)

function check(theForm) {

  if(theForm.plz.value.length == 0) {

    alert("Feld PLZ nicht ausgefüllt");

    theForm.plz.focus();

    return (false);

  }

  if(theForm.ort.value.length == 0) {

    alert("Feld Ort nicht ausgefüllt");

    theForm.ort.focus();

    return (false);

  }

}

HTML (form.htm)
<script type="text/javascript" src="check.js"></script>

<form action="suche.htm" method="post" name="suche">

  <input type="text" name="plz" />       

  <input type="text" name="ort" />

  <input type="button" name="submit" onclick="return check(this)" />

</form>

Jedoch funktioniert es nicht. Die Fehlermeldung lautet immer: "plz.value" ist NULL oder kein Objekt".

Soweit ist mir klar was die Fehlermeldung mir sagen will, aber plz ist doch eindeutig da. Würde gerne verstehen woran das liegt.

Danke im Vorraus und Gruß Tobias

dein "theForm" gibt es nicht. Wenn, dann sollte es mit (suche.plz.value == "") versuchen.

ich dachte ich übergebe mit bei "check(this)" das aktuelle Formular als Javascript Objekt und "theForm" ist doch der Parameter von "check".

es muss so heissen:


<script type="text/javascript" src="check.js"></script>

<form action="suche.htm" method="post" name="suche">

  <input type="text" name="plz" />       

  <input type="text" name="ort" />

  <input type="button" name="submit" onclick="return check(this.form)" />

</form>

Wenn du mehere Buttons hast, kannst dus auch so machen:

<script type="text/javascript" src="check.js"></script>

<form action="suche.htm" method="post" name="suche" onSubmit="return check(this)">

  <input type="text" name="plz" />       

  <input type="text" name="ort" />

  <input type="button" name="submit" />

</form>

schöner ist es allerdings mit Smarty und SmartyValidate - Serverseitig zu validieren:

siehe: http://smarty.php.net

Sorry, verpeilt. War aber, wie kills festgestellt hat, eh nicht ganz richtig.

Hey!

"this.form" funktioniert, vielen Dank kills und kLeiner_HobBes!

Gruß Tobias

Hey!

"this.form" funktioniert, vielen Dank kills und kLeiner_HobBes!

Gruß Tobias

bitte bitte.

this alleine hat nicht funktioniert, weil wenn du innerhalb eines <input ../> den this zeiger aufrufst, dann bekommst du im JavaScript das Object des Inputs, nicht des Formulars. Aber laut selfHTML kann man bei allen Formularelementen this.form angeben und schon klappts.

....

:marine

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.