Form validation helps check if the user filled the form correctly before sending it. It stops mistakes and makes sure the data is good.
Form validation in Svelte
<script> let name = ''; let email = ''; let errors = {}; function validate() { errors = {}; if (!name) errors.name = 'Name is required'; if (!email) errors.email = 'Email is required'; else if (!email.includes('@')) errors.email = 'Email must include @'; return Object.keys(errors).length === 0; } function submitForm() { if (validate()) { alert('Form is valid!'); } } </script> <form on:submit|preventDefault={submitForm} aria-label="User form"> <label for="name">Name:</label> <input id="name" type="text" bind:value={name} aria-describedby="name-error" /> {#if errors.name} <div id="name-error" role="alert" style="color: red;">{errors.name}</div> {/if} <label for="email">Email:</label> <input id="email" type="email" bind:value={email} aria-describedby="email-error" /> {#if errors.email} <div id="email-error" role="alert" style="color: red;">{errors.email}</div> {/if} <button type="submit">Submit</button> </form>
Use bind:value to connect input fields to variables.
Use on:submit|preventDefault to handle form submission without page reload.
username.<input type="text" bind:value={username} />function validate() { if (!email.includes('@')) { errors.email = 'Email must include @'; } }
submitForm instead.<form on:submit|preventDefault={submitForm}>...</form>This Svelte component shows a simple form with name and email fields. It checks if the fields are filled and if the email contains '@'. If there are errors, it shows messages in red below the inputs. When the form is valid, it shows an alert.
<script> let name = ''; let email = ''; let errors = {}; function validate() { errors = {}; if (!name) errors.name = 'Name is required'; if (!email) errors.email = 'Email is required'; else if (!email.includes('@')) errors.email = 'Email must include @'; return Object.keys(errors).length === 0; } function submitForm() { if (validate()) { alert('Form is valid!'); } } </script> <form on:submit|preventDefault={submitForm} aria-label="User form"> <label for="name">Name:</label> <input id="name" type="text" bind:value={name} aria-describedby="name-error" /> {#if errors.name} <div id="name-error" role="alert" style="color: red;">{errors.name}</div> {/if} <label for="email">Email:</label> <input id="email" type="email" bind:value={email} aria-describedby="email-error" /> {#if errors.email} <div id="email-error" role="alert" style="color: red;">{errors.email}</div> {/if} <button type="submit">Submit</button> </form>
Always use aria-describedby and role="alert" for error messages to help screen readers.
Use preventDefault to stop page reload on form submit.
Validation can be expanded with more rules as needed.
Form validation checks user input before sending data.
Svelte uses bind:value to connect inputs to variables.
Show error messages clearly and accessibly for better user experience.
Practice
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 AQuick Check:
Form validation = check input [OK]
- Confusing validation with styling
- Thinking validation loads data
- Mixing validation with component creation
Solution
Step 1: Recall Svelte binding syntax
Svelte usesbind: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 BQuick Check:
bind:value with braces = correct [OK]
- Omitting curly braces
- Using colon instead of equals
- Using value= instead of bind:value=
<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>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 AQuick Check:
Empty input triggers error message [OK]
- Ignoring preventDefault causing page reload
- Expecting form to submit without error
- Confusing syntax error with logic error
<script>
let password = '';
let error = '';
function check() {
if(password.length < 6) {
error = 'Password too short';
}
}
</script>
<form on:submit={check}>
<input bind:value={password} type="password" />
<button type="submit">Login</button>
<p>{error}</p>
</form>Solution
Step 1: Review submit event handling
The form's submit event calls check but does not prevent default, so page reloads.Step 2: Identify missing preventDefault
Without preventDefault, error message disappears on reload, breaking validation UX.Final Answer:
Missing preventDefault on submit event -> Option CQuick Check:
Submit needs preventDefault to stop reload [OK]
- Forgetting preventDefault causes page reload
- Thinking binding syntax is wrong when it is correct
- Assuming error variable is missing
Solution
Step 1: Understand accessibility for errors
Screen readers need to be notified of error changes dynamically.Step 2: Use aria-live attribute
Addingaria-live="polite"tells assistive tech to announce updates politely.Final Answer:
Add aria-live="polite" to error message container -> Option DQuick Check:
aria-live improves screen reader announcements [OK]
- Relying only on color changes (not accessible)
- Hiding errors visually removes info from users
- Removing labels harms form clarity
Ready to prove it?
Pass the test and this pattern moves unknown
