Form validation
📖 Scenario: You are building a simple user registration form on a website. The form needs to check that the user enters their name and email correctly before submitting.
🎯 Goal: Create a Svelte component that has a form with name and email input fields. Add validation so the form shows error messages if the name is empty or the email is not valid. The submit button should only be enabled when the form is valid.
📋 What You'll Learn
Create
name and email variables to hold input valuesAdd a
validEmail regular expression to check email formatWrite a function
validateForm that returns true only if name is not empty and email matches validEmailBind inputs to variables and disable submit button if
validateForm() returns falseShow error messages below inputs when invalid
💡 Why This Matters
🌍 Real World
Forms are everywhere on websites and apps. Validating user input helps prevent errors and improves user experience.
💼 Career
Knowing how to build forms with validation is a key skill for frontend developers working with modern frameworks like Svelte.
Progress0 / 4 steps