npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

alpinejs-form-validation

v1.1.0

Published

Adds client side form/input validation powered by Alpine JS ✅

Downloads

1,560

Readme

Alpine JS Form Validation

Add client side form validation with Alpine JS 🎉

Install

With a CDN

<script
  defer
  src="https://unpkg.com/alpinejs-form-validation@latest/dist/validation.min.js"
></script>

<script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>

With a Package Manager

yarn add -D alpinejs-form-validation

npm install -D alpinejs-form-validation
import Alpine from 'alpinejs'
import validation from 'alpinejs-form-validation'

Alpine.plugin(validation)

Alpine.start()

Example

This example uses Tailwind CSS for styling but that is not mandatory.

<form
  @submit.prevent="$dispatch('validate')"
  x-data="{ contactName: '', contactAge: '', contactMessage: '', contactTerms: false }"
  class="max-w-xl mx-auto space-y-4"
>
  <div
    x-data="{ required: false }"
    @error="required = $valid($event.detail, 'required')"
  >
    <label for="contactName" class="font-medium"> Name </label>

    <input
      id="contactName"
      type="text"
      x-model="contactName"
      x-validation.required="contactName"
      class="w-full mt-1 rounded data-[validation-valid=true]:border-green-500 data-[validation-valid=false]:border-red-500"
    />

    <small x-cloak x-show="required" class="text-red-600 mt-1 block">
      Need a name
    </small>
  </div>

  <div
    x-data="{ minAge: false, maxAge: false }"
    @error="
      minAge = $valid($event.detail, 'min')
      maxAge = $valid($event.detail, 'max')
    "
  >
    <label for="contactAge" class="font-medium"> Age </label>

    <input
      id="contactAge"
      type="number"
      x-model="contactAge"
      x-validation.min.18.max.24="contactAge"
      class="w-full rounded mt-1 data-[validation-valid=true]:border-green-500 data-[validation-valid=false]:border-red-500"
    />

    <small x-cloak x-show="minAge" class="text-red-600 mt-1 block">
      Must be at least 18 years old
    </small>

    <small x-cloak x-show="maxAge" class="text-red-600 mt-1 block">
      Can't be older than 24 years old
    </small>
  </div>

  <div
    x-data="{ minLetters: false, maxLetters: false }"
    @error="
      minLetters = $valid($event.detail, 'minLength')
      maxLetters = $valid($event.detail, 'maxLength')
    "
  >
    <label for="contactMessage" class="font-medium"> Message </label>

    <textarea
      id="contactMessage"
      x-model="contactMessage"
      x-validation.min:length.10.max:length.50="contactMessage"
      class="rounded w-full mt-1 data-[validation-valid=true]:border-green-500 data-[validation-valid=false]:border-red-500"
    ></textarea>

    <small x-cloak x-show="minLetters" class="text-red-600 mt-1 block">
      Must be at least 10 characters
    </small>

    <small x-cloak x-show="maxLetters" class="text-red-600 mt-1 block">
      Can't be more than 50 characters
    </small>
  </div>

  <div
    x-data="{ isAccepted: false }"
    @error="isAccepted = $valid($event.detail, 'checked')"
  >
    <label for="contactTerms" class="inline-flex items-center gap-2">
      <input
        id="contactTerms"
        type="checkbox"
        value="true"
        x-model="contactTerms"
        x-validation.checked="contactTerms"
        class="size-5 rounded data-[validation-valid=false]:border-red-500"
      />

      <span class="font-medium">I accept the terms and conditions</span>
    </label>

    <small x-cloak x-show="isAccepted" class="text-red-600 mt-1 block">
      Must accept
    </small>
  </div>

  <button class="px-5 py-2.5 rounded bg-blue-600 font-medium text-white">
    Submit
  </button>
</form>

Functionality

Breaking down the example we have the following.

$dispatch('validate')

Emitting the event validate will trigger each input within the element that the event was emitted from to run through the validation checks.

x-validation.required="contactName"

Here we are setting up the directive x-validation and passing the modifier required which says, the value of contactName must exist to pass validation.

Validation Options

  • required
  • min.X (Where "X" is an integer)
  • max.X (Where "X" is an integer)
  • min:length.X (Where "X" is an integer)
  • max:length.X (Where "X" is an integer)

@error="required = $valid($event.detail, 'required')"

Here we have a few things going on.

We're listing for the @error event which is emitted once the input has run through the validation checks. We're then setting required to either true/false based on the response from the magic helper $valid.

$valid

This is a magic helper which returns true/false based on the validation status of the input. You pass in the $event.detail which comes from the @error event and the validation option, in the example that's required.

Styling

You could style the inputs like this:

[data-validation-valid='false'] {
  border-color: red;
}

[data-validation-valid='true'] {
  border-color: green;
}

Stats