freeCodeCamp/curriculum/challenges/chinese-traditional/14-responsive-web-design-22/learn-html-forms-by-buildin.../60fabf0dd4959805dbae09e6.md

3.0 KiB

id title challengeType dashedName
60fabf0dd4959805dbae09e6 步驟 28 0 step-28

--description--

在第一個 label 在添加另一個,其文本爲 Input your age (years):。 在它後面添加一個 input 並設置其 typenumber

由於我們不希望 13 歲以下的用戶註冊,給 input 添加一個 min 屬性並設置其值爲 13。 此外,我們假設年齡超過 120 歲的用戶不會來註冊,添加一個 max 屬性,並設置其值爲 120

現在,如果提交的表單裏面的年齡超過了這個範圍,會出現一個警告,並阻止提交。 試一下吧。

--hints--

應該在第三個 fieldset 內已存在的 label 後面在添加一個 label

assert.exists(document.querySelector('fieldset:nth-child(3) > label + label'));

label 的文字應該爲 Input your age (years):

assert.equal(document.querySelector('fieldset:nth-child(3) > label:nth-child(2)')?.textContent?.trim(), 'Input your age (years):');

label 內應該有一個 typenumberinput

assert.exists(document.querySelector('fieldset:nth-child(3) > label:nth-child(2) > input[type="number"]'));

input 應該有一個 min 屬性且其值爲 13

assert.equal(document.querySelector('fieldset:nth-child(3) > label:nth-child(2) > input[type="number"]')?.min, '13');

input 應該有一個 max 屬性且其值爲 120

assert.equal(document.querySelector('fieldset:nth-child(3) > label:nth-child(2) > input[type="number"]')?.max, '120');

--seed--

--seed-contents--

<!DOCTYPE html>
<html>
  <head>
    <title>Registration Form</title>
      <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <h1>Registration Form</h1>
    <p>Please fill out this form with the required information</p>
    <form action='https://register-demo.freecodecamp.org'>
      <fieldset>
        <label>Enter Your First Name: <input type="text" required /></label>
        <label>Enter Your Last Name: <input type="text" required /></label>
        <label>Enter Your Email: <input type="email" required /></label>
        <label>Create a New Password: <input type="password" pattern="[a-z0-5]{8,}" required /></label>
      </fieldset>
      <fieldset>
        <label><input type="radio" name="account-type" /> Personal Account</label>
        <label><input type="radio" name="account-type" /> Business Account</label>
        <label>
          <input type="checkbox" required /> I accept the <a href="https://www.freecodecamp.org/news/terms-of-service/">terms and conditions</a>
              </label>
      </fieldset>
--fcc-editable-region--
      <fieldset>
        <label>Upload a profile picture: <input type="file" /></label>

      </fieldset>
--fcc-editable-region--
      <input type="submit" value="Submit" />
    </form>
  </body>
</html>
body {
  width: 100%;
  height: 100vh;
  margin: 0;
  background-color: #1b1b32;
    color: #f5f6f7;
}

label {
    display: block;
    margin: 0.5rem 0;
}