0
0
Svelteframework~20 mins

Form validation in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Form Validation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when submitting a form with empty required fields?

Consider this Svelte form with two required inputs. What happens when the user submits the form without filling any fields?

Svelte
<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>
AThe form shows the error message 'Please fill all required fields.' below the button.
BThe browser shows a native tooltip error for the first empty required input.
CThe form submits successfully and clears the inputs.
DNothing happens; the form silently fails without feedback.
Attempts:
2 left
💡 Hint

Look at the handleSubmit function and the error variable usage.

📝 Syntax
intermediate
2:00remaining
Which option correctly validates an email input in Svelte?

Which code snippet correctly updates an emailError message when the email input is invalid?

Svelte
<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>
A
function validateEmail() {
  emailError = email.match(/^[^@]+@[^@]+\.[^@]+$/) ? '' : 'Invalid email address';
}
B
function validateEmail() {
  emailError = email.includes('@') ? '' : 'Invalid email address';
}
C
function validateEmail() {
  if (!email.includes('@')) {
    emailError = 'Invalid email address';
  } else {
    emailError = '';
  }
}
D
function validateEmail() {
  emailError = email.length &gt; 5 ? '' : 'Invalid email address';
}
Attempts:
2 left
💡 Hint

Check which option uses a proper email pattern to validate.

state_output
advanced
2:00remaining
What is the value of 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?

Svelte
<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" />
Afalse
Bundefined
Cnull
Dtrue
Attempts:
2 left
💡 Hint

Check the derived store logic and the input values.

🔧 Debug
advanced
2:00remaining
Why does this Svelte form not show validation errors on submit?

Review this Svelte form code. The form submits even when inputs are empty, and no error message appears. What is the cause?

Svelte
<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>
AThe error variable is not reactive, so it never updates.
BThe form submits because the submit event is not prevented, so the page reloads before error shows.
CThe input is missing a name attribute, so validation fails silently.
DThe error message is outside the form, so it is not displayed.
Attempts:
2 left
💡 Hint

Check the form submission event handling.

🧠 Conceptual
expert
3:00remaining
Which approach best ensures accessible form validation feedback in Svelte?

Choose the best practice for showing validation errors that screen readers announce immediately after user input changes.

AUse <code>aria-hidden="true"</code> on error messages to avoid distraction.
BUse <code>role="alert"</code> on the error message and update it only on form submit.
CUse <code>aria-live="assertive"</code> on the error message container and update error text reactively.
DShow errors only as placeholder text in inputs without ARIA roles.
Attempts:
2 left
💡 Hint

Think about how screen readers detect live changes.