html-tutorial

HTML Concepts for beginners

This project is maintained by pushdev-code

Forms and validations

image

How to structure a web form

    <form action="">
        <!--form content-->
    </form>

Some tips:

<label for="name">
        Name: <input type="text" id="name" name="user_name">
        <abbr title="required" aria-label="required">*</abbr>
 </label>
<fieldset>
        <legend>Choose your city:</legend>
        <select id="city">
            <option value="Tunja">Tunja</option>
            <option value="Bogota">Bogota</option>
            <option value="Cali">Cali</option>
            <option value="Bucaramanga">Bucaramanga</option>
            <option value="Medellin">Medellin</option>
            <option value="Pasto">Pasto</option>
        </select>
 </fieldset>
  <fieldset>
        <legend>Choose your bootcamp:</legend>
        <input id="basic" type="radio" name="bootcamp" value="basic">
        <label for="basic">Basic web development</label>
        <input id="advanced" type="radio" name="bootcamp" value="advanced">
        <label for="advanced">Advanced web development</label>
    </fieldset>

Validation

There are two different types of client-side validation that you’ll encounter on the web:

<form>
    <label for="number">How old are you?</label>
    <input type="number" id="number" name="amount" value="1" min="1" max="100">

    <label for="t2">What's your favorite sport<abbr title="This field is mandatory" aria-label="required">*</abbr></label>
        <input type="text" id="t2" name="sport" list="l2" required pattern="[Ss]occer|[Bb]asketball|[Hh]ockey|[Tt]ennis|[Vv]olleyball">
        <datalist id="l1">
          <option>Soccer</option>
          <option>Basketball</option>
          <option>Hockey</option>
          <option>Tennis</option>
          <option>Volleyball</option>
        </datalist>

       <p>
          <label for="t3">Leave your personal description</label>
          <textarea id="t3" name="msg" maxlength="100" rows="5"></textarea>
       </p>
</form>

Other validations:

Let’s see the difference: Form validation

Submit - Reset buttons

  <button type="submit">Validate the payment</button>

  <button type="reset">Reset</button>

Should I specify the button type?

image

<form>
    <button>I will submit the form when clicked!</button>
</form>

//vs

<form>
    <button type='button'>I won't!</button>
</form>

Exercise

  1. Inside your 4-forms-validations folder create a file named form.html.
  2. Build in HTML a simple form with semantic structure. The form must have the following elements:
  1. These fields must be required.
  2. Push the changes to your local copy of the repo (Pull request).

Source