SheTech
  • SheTech JS bootcamp
  • Level 1 - awesome tasks
    • Introduction to Awesome Tasks
    • Step 1: HTML
    • Step 2: playing with the DOM
    • Step 3: add a new task
    • Step 4: dynamic list of tasks
    • Step 5: adding new items
    • Step 6: removing items
    • Step 7: add some style
    • Step 8: marking items as done
    • Step 9: saving items in the localstorage
    • Step 10: adding filters and bulk actions
    • Step 11: add counters
    • From Awesome Task to the next big thing!
  • Level 2 - timeline
    • Live Timeline
    • Step 1: basic HTML page
    • Step 2: add util library
    • Step 3: show all posts
    • Step 4: add form
    • Step 5: collect form data
    • Step 6: create a new post
    • Step 7: validating posts
    • Step 8: refactoring
    • Step 9: refresh button
    • Step 10: auto refresh
  • Code Editors
  • Useful resources
  • API Reference
  • Glossary
Powered by GitBook
On this page
  • Validating new posts.
  • What have we done here?

Was this helpful?

  1. Level 2 - timeline

Step 7: validating posts

PreviousStep 6: create a new postNextStep 8: refactoring

Last updated 4 years ago

Was this helpful?

Validating new posts.

We learnt how to create new posts. Now we may want to add some validation rules for them. You do not want to get empty posts or very short ones.

We're going to use client side validation to check if either title or body are longer than 4 characters, for example.

Also, we want to show some kind of warning when a user tries to create a post that does not pass our super cool validation rule.

Replace:

<form id="post-form">
  <div class="form-group">
    <label for="post-title" class="control-label">Post Title</label>
    <input type="text" class="form-control" id="post-title" placeholder="A new post"/>
  </div>
  <div class="form-group">
    <label for="post-body" class="control-label">Post Content</label>
    <textarea type="text" class="form-control" id="post-body" placeholder="A new Body"></textarea>
  </div>
  <button type="submit" class="btn btn-primary">Post</button>
</form>

with

<form id="post-form">
  <div class="form-group">
    <label for="post-title" class="control-label">Post Title</label>
    <input type="text" class="form-control" id="post-title" placeholder="A new post" minlength="4"/>
  </div>
  <div class="form-group">
    <label for="post-body" class="control-label">Post Content</label>
    <textarea type="text" class="form-control" id="post-body" placeholder="A new Body" minlength="4"></textarea>
  </div>
  <button type="submit" class="btn btn-primary">Post</button>
</form>

What have we done here?

We have added a property minlength to both text inputs. This allows HTML5 form controls to check the length of the text we're inputting and to automatically act at elements class for displaying validation errors.

What is client side validation?