📖 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 values
Add a validEmail regular expression to check email format
Write a function validateForm that returns true only if name is not empty and email matches validEmail
Bind inputs to variables and disable submit button if validateForm() returns false
Show 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
1
Set up form data variables
Create two variables called name and email and set both to empty strings.
Svelte
Hint
Use let to create variables in Svelte.
2
Add email validation pattern
Add a variable called validEmail and set it to the regular expression /^\S+@\S+\.\S+$/ to check email format.
Svelte
Hint
Use const for the regular expression since it won't change.
3
Create form validation function
Write a function called validateForm that returns true only if name is not empty and email matches validEmail. Otherwise, return false.
Svelte
Hint
Use name.trim() !== "" to check name is not empty and validEmail.test(email) to check email.
4
Build form with validation feedback
Create a form with two inputs bound to name and email. Add error messages below each input that show if the input is invalid. Disable the submit button if validateForm() returns false.
Svelte
Hint
Use Svelte's bind:value to connect inputs to variables. Use {#if} blocks to show errors. Disable the button with disabled={!validateForm()}.
Practice
(1/5)
1. What is the main purpose of form validation in a Svelte application?
easy
A. To check user input before sending data
B. To style the form elements
C. To load data from the server
D. To create new components
Solution
Step 1: Understand form validation role
Form validation ensures user input is correct before submission.
Step 2: Identify the correct purpose
Checking input prevents errors and improves user experience.
Final Answer:
To check user input before sending data -> Option A
Quick Check:
Form validation = check input [OK]
Hint: Validation means checking input before sending [OK]
Common Mistakes:
Confusing validation with styling
Thinking validation loads data
Mixing validation with component creation
2. Which Svelte syntax correctly binds an input's value for validation?
easy
A.
B.
C.
D.
Solution
Step 1: Recall Svelte binding syntax
Svelte uses bind:value={variable} to bind input values.
Step 2: Identify correct syntax
<input bind:value={name} /> uses correct curly braces and binding keyword.
Final Answer:
<input bind:value={name} /> -> Option B
Quick Check:
bind:value with braces = correct [OK]
Hint: Use bind:value with curly braces for binding [OK]
Common Mistakes:
Omitting curly braces
Using colon instead of equals
Using value= instead of bind:value=
3. What will be the output when submitting this Svelte form if the input is empty?
<script>
let email = '';
let error = '';
function validate() {
if (!email) {
error = 'Email is required';
} else {
error = '';
}
}
</script>
<form on:submit|preventDefault={validate}>
<input bind:value={email} type="email" />
<button type="submit">Submit</button>
<p>{error}</p>
</form>
medium
A. Error message 'Email is required' is shown
B. Form submits with no error message
C. Syntax error prevents form rendering
D. Form reloads the page
Solution
Step 1: Analyze validation function
If email is empty, error is set to 'Email is required'.
Step 2: Check form submission behavior
Submit event is prevented from default, so page does not reload, and error message shows.
Final Answer:
Error message 'Email is required' is shown -> Option A