Consider this Svelte form with two required inputs. What happens when the user submits the form without filling any fields?
<script> let name = ''; let email = ''; let error = ''; function handleSubmit() { if (!name || !email) { error = 'Please fill all required fields.'; } else { error = ''; } } </script> <form on:submit|preventDefault={handleSubmit} aria-label="User form"> <label for="name">Name:</label> <input id="name" type="text" bind:value={name} aria-required="true" /> <label for="email">Email:</label> <input id="email" type="email" bind:value={email} aria-required="true" /> <button type="submit">Submit</button> {#if error} <p role="alert" style="color: red;">{error}</p> {/if} </form>
Look at the handleSubmit function and the error variable usage.
The handleSubmit function checks if name or email is empty. If so, it sets the error message, which is shown below the button. The form does not submit because of preventDefault.
Which code snippet correctly updates an emailError message when the email input is invalid?
<script> let email = ''; let emailError = ''; function validateEmail() { // validation logic here } </script> <input type="email" bind:value={email} on:input={validateEmail} aria-describedby="email-error" /> <p id="email-error" role="alert" style="color: red;">{emailError}</p>
Check which option uses a proper email pattern to validate.
Option A uses a regular expression to check the email format correctly. Option A only checks for '@' presence, which is insufficient. Option A does not clear the error if valid. Option A checks length, which is not reliable.
formValid after input changes?Given this Svelte code, what is the value of formValid after the user types 'user@example.com' in the email input and 'John' in the name input?
<script> import { derived, writable } from 'svelte/store'; const name = writable(''); const email = writable(''); const formValid = derived( [name, email], ([$name, $email]) => $name.trim().length > 0 && /^[^@]+@[^@]+\.[^@]+$/.test($email) ); </script> <input type="text" bind:value={$name} aria-label="Name input" /> <input type="email" bind:value={$email} aria-label="Email input" />
Check the derived store logic and the input values.
The formValid store returns true only if name is not empty and email matches the regex. Both inputs meet these conditions, so formValid is true.
Review this Svelte form code. The form submits even when inputs are empty, and no error message appears. What is the cause?
<script> let username = ''; let error = ''; function submitForm(event) { event.preventDefault(); if (!username) { error = 'Username required'; } } </script> <form on:submit={submitForm} aria-label="Username form"> <input type="text" bind:value={username} aria-required="true" /> <button type="submit">Submit</button> {#if error} <p role="alert" style="color: red;">{error}</p> {/if} </form>
Check the form submission event handling.
The submitForm function does not call event.preventDefault(), so the form submits normally and reloads the page before the error message can show.
Choose the best practice for showing validation errors that screen readers announce immediately after user input changes.
Think about how screen readers detect live changes.
Using aria-live="assertive" ensures screen readers announce error messages immediately when they appear. role="alert" also works but updating only on submit delays feedback. Placeholder text is not accessible feedback. Hiding errors prevents screen readers from reading them.