[HTML] form tag validation with required

Quick HTML Validation in Forms with required

When building a signup or login form, you often want to ensure users enter values for certain fields (like email and password) before sending a request to the server. A simple way to do this is using HTML5 form validation with the required attribute.

Example: Sign-up Form

<form action="/blog/sign_up" method="post" class="d-flex align-items-center">
  <input
    type="email"
    name="user_email"
    class="form-control me-2"
    placeholder="Email"
    required
  >
  <input
    type="password"
    name="password"
    class="form-control me-2"
    placeholder="Password"
    required
  >
  <button type="submit" class="btn btn-outline-primary btn-lg">
    Sign up
  </button>
</form>

In this example:

  • required on each input field triggers the browser’s built-in validation.

  • If a user clicks Sign up without filling both fields, the form won’t submit.

  • No extra JavaScript or backend logic is needed to stop empty submissions.

Note : Not a security measure: Always re-validate on the server side in case someone circumvents client checks.

Comments