Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    JSJavaScript

    Getting started

    • What is JavaScript?
    • Variables: let, const and var
    • Data types
    • Type conversion

    Operators and strings

    • Arithmetic and assignment
    • Comparison and logic
    • Strings and template literals
    • String methods
    • Numbers and Math

    Control flow

    • Conditions: if and else
    • switch and the ternary operator
    • Loops: for and while
    • Challenge: FizzBuzz

    Functions

    • Functions
    • Arrow functions
    • Parameters: default, rest and spread
    • Scope and closures
    • Callbacks and higher-order functions

    Arrays

    • Array basics
    • map, filter and find
    • reduce and sorting
    • Destructuring

    Objects

    • Object basics
    • Copies and references
    • Methods and this
    • JSON and optional chaining

    Classes and OOP

    • Classes
    • Inheritance

    JavaScript in the page

    • Controlling the page (DOM)
    • Creating elements
    • Events
    • Forms and validation
    • Saving in the browser (localStorage)

    Errors and async

    • Handling errors: try and catch
    • Timers and the event loop
    • Promises
    • async and await
    • Fetching data from an API

    Practical tools and techniques

    • Searching with regular expressions
    • Cancellation and stale results
    • Testing and debugging JavaScript

    Modern JavaScript and a project

    • Map and Set
    • Dates and localised formatting
    • Modules: import and export
    • Project: a complete to-do app
    JavaScript

    JavaScript in the page · Lesson 32 of 45

    Forms and validation

    Read form data, validate it, and show clear errors.

    8 minutes2 games

    Listen for submit on the form itself (not the button's click) so it also works when someone presses Enter. Read the data with new FormData(form).

    HTML
    <form id="signup" novalidate>
      <p><label>Email <input name="email" type="email"></label></p>
      <p><label>Password <input name="password" type="password"></label></p>
      <p id="error" style="color: crimson"></p>
      <button>Sign up</button>
    </form>
    
    <script>
      const form = document.querySelector("#signup")
      const error = document.querySelector("#error")
    
      form.addEventListener("submit", (e) => {
        e.preventDefault()
        const data = Object.fromEntries(new FormData(form))
    
        if (!data.email.includes("@")) return (error.textContent = "Enter a valid email")
        if (data.password.length < 8) return (error.textContent = "Password needs 8+ characters")
    
        error.textContent = ""
        console.log("Sending", data)
      })
    </script>

    Try submitting it empty, then with a bad email, then correctly.

    Important

    Browser validation is for convenience only (instant feedback). The server must always validate again, because anyone can send requests to it without your page.

    Interview question

    What's the difference between e.preventDefault() and e.stopPropagation()?

    Complete the code

    Stop the form from reloading the page.

    form.addEventListener("submit", (e) => {
      e.()
    })

    Exercise

    Write isValidPassword(p) returning true if it's 8+ characters and contains a digit. Print isValidPassword("secret12").

    Expected output: true

    JavaScript

    Learn more from

    • javascript.info: Form properties and methodsjavascript.info
    • MDN: Client-side form validationdeveloper.mozilla.org
    • MDN: FormDatadeveloper.mozilla.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousEventsNextSaving in the browser (localStorage)
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.