Form Validation

Client-side form validation with custom error messages
Learn how to validate user input in real-time using hyperscript's conditional logic and event handling. Build better forms with instant feedback!
validation if/else add/remove classes forms

Registration Form

Requirements:
  • Email must contain @ symbol
  • Password must be at least 8 characters
  • Password confirmation must match
❌ Email must contain @ symbol
❌ Password must be at least 8 characters
❌ Passwords do not match
Account created successfully.
Welcome aboard.

The Code

Email Validation:

<input _="on input if my value contains '@' add .valid to me remove .error from me hide #email-error else add .error to me remove .valid from me show #email-error end" />

Password Length Check:

<input _="on input if my value's length >= 8 add .valid to me remove .error from me hide #password-error else add .error to me remove .valid from me show #password-error end" />

Password Confirmation:

<input _="on input if my value == #password's value add .valid to me hide #confirm-error else add .error to me show #confirm-error end" />

Form Submission Check:

<button _="on click if #email's value contains '@' and #password's value's length >= 8 and #confirm's value == #password's value show #success hide #signup-form else log 'Form has validation errors' end"> Submit </button>

How it works:

Key Concepts:

Validation Patterns:

Advanced Validation Examples:

<!-- Phone number validation --> <input _="on input if my value matches /^\d{3}-\d{3}-\d{4}$/ add .valid else add .error end" /> <!-- Age range validation --> <input type="number" _="on input if my value >= 18 and my value <= 100 add .valid else add .error end" /> <!-- Checkbox required --> <input type="checkbox" _="on change if my checked enable #submit-btn else disable #submit-btn end" />

Try it yourself: