66 lines
2.1 KiB
HTML
66 lines
2.1 KiB
HTML
<!doctype html>
|
|
<html lang="de">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<title>Übung 23: Von guten und bösen Eingaben</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" />
|
|
</head>
|
|
<body>
|
|
<main>
|
|
<div class="container py-5">
|
|
<div class="alert alert-primary">
|
|
<h2>Übung 23: Von guten und bösen Eingaben</h2>
|
|
<ol class="list">
|
|
<li>
|
|
Nimm Angaben zu Vorname, Nachname, Alter und dem liebsten Heißgetränk eines Anwenders entgegen und
|
|
speichere diese in Variablen.
|
|
</li>
|
|
<li>
|
|
Diese Eingaben sollst du nun folgendermaßen validieren:
|
|
<ul class="list">
|
|
<li>Vorname und Nachname müssen zwischen 2 und 100 Zeichen lang sein.</li>
|
|
<li>Das Alter muss eine Zahl unter 150 sein.</li>
|
|
<li>Die Eingabe für das Getränk muss c (coffee) oder t (tea) sein.</li>
|
|
</ul>
|
|
</li>
|
|
</ol>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
<script>
|
|
'use strict';
|
|
|
|
// Aufgabe
|
|
const firstName = prompt('Wilkommen. Wie heißen Sie mit Vornamen?');
|
|
const lastName = prompt('Und mit Nachnamen?');
|
|
const age = Number(prompt('Wie alt sind Sie?'));
|
|
const favoriteHotDrink = prompt('Und was ist ihr liebstes Heißgetränk? tee (t) oder kaffee (c)?');
|
|
|
|
let errorMessage = '';
|
|
|
|
if (lastName.length < 2 || lastName.length > 100) {
|
|
errorMessage += 'name ';
|
|
}
|
|
|
|
if (firstName.length < 2 || firstName.length > 100) {
|
|
errorMessage += 'first name ';
|
|
}
|
|
|
|
if (favoriteHotDrink !== 'c' && favoriteHotDrink !== 't') {
|
|
errorMessage += 'favoriteHotDrink ';
|
|
}
|
|
|
|
if (isNaN(age) || age >= 150) {
|
|
errorMessage += 'age ';
|
|
}
|
|
|
|
if (errorMessage === '') {
|
|
console.log('OK!');
|
|
} else {
|
|
console.log(`KO! The following entries are invalid: ${errorMessage}`);
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|