Newer
Older
hello-programmer-world / src / sample / js / work / age-checker.html
@h.sakamoto h.sakamoto 13 days ago 1 KB work js
<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>年齢判定プログラム</title>
  <style>
    body {
      font-family: sans-serif;
      padding: 20px;
    }
    .container {
      max-width: 400px;
      margin: 0 auto;
    }
    .form-group {
      margin-bottom: 15px;
    }
    label {
      display: block;
      margin-bottom: 5px;
      font-weight: bold;
    }
    input {
      width: 100%;
      padding: 8px;
      font-size: 16px;
      border: 2px solid #ddd;
      border-radius: 4px;
    }
    button {
      padding: 10px 20px;
      font-size: 16px;
      cursor: pointer;
      background-color: #4CAF50;
      color: white;
      border: none;
      border-radius: 4px;
      margin-top: 10px;
    }
    button:hover {
      background-color: #45a049;
    }
    #result {
      margin-top: 20px;
      padding: 15px;
      border-radius: 4px;
      font-size: 18px;
      font-weight: bold;
    }
    .adult {
      background-color: #d4edda;
      color: #155724;
    }
    .minor {
      background-color: #fff3cd;
      color: #856404;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>年齢判定プログラム</h1>
    <div class="form-group">
      <label for="age">年齢を入力してください:</label>
      <input type="number" id="age" min="0" max="150" value="18">
    </div>
    <button id="checkButton">判定する</button>
    <div id="result"></div>
  </div>

  <script>
    const ageInput = document.querySelector("#age");
    const checkButton = document.querySelector("#checkButton");
    const result = document.querySelector("#result");

    checkButton.addEventListener("click", () => {
      const age = Number(ageInput.value);

      if (age >= 20) {
        result.textContent = "成人です";
        result.className = "adult";
      } else {
        result.textContent = "未成年です";
        result.className = "minor";
      }
    });
  </script>
</body>
</html>