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

jquery.csssr.validation

v0.0.26

Published

Universal validation plugin

Downloads

58

Readme

jquery.csssr.validation

jquery.csssr.validation is an universal validation plugin, which requires zero JS code to validate the forms on your project. Simply connect it, add a couple of attributes to your forms and here you go, it does all you need.

bower i jquery.csssr.validation
npm i jquery.csssr.validation

Important: If haven't done it yet, please upgrade to version 0.0.26, it contains an important fix for the URL validation. The regular expression ^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$ was vulnerable to REDOS, so it was replaced with a function which uses the URL API and checks the protocol to match http(s):. Thanks to James Davis for the finding. Also consider upgrading your own projects if you reused the mentioned regular expression anywhere else.


  1. Quickstart
  2. Validation Features
  1. Visualizing validation results
  1. Understanding validation events
  2. Silent validation
  3. Custom validation containers and validation with multiple steps
  4. Global plugin configuration
  5. Overriding of global options
  1. Options reference

Quickstart

Let's begin with a simple registration form with four fields: username, email, password and password confirmation.

  1. To initialize the plugin, add the data-validate attribute to your form.
  2. Add the required attribute to the fields you want to be checked for empty values.
  3. Set the type of the email field to email.
  4. Using the data-invalid-class attribute, define the CSS class which will be added to the field when its value is empty.
  5. Link the password & password confirmation fields with the data-equal-to attribute:
<form
    id="frmRegister"
    action="#"
    method="post"
    data-validate
    data-invalid-class="invalid">

    <input type="text" name="username" placeholder="Username" required />
    <input type="email" name="username" placeholder="Email" required />
    <input id="txtPassword" type="password" name="password" placeholder="Password" required />
    <input type="password" name="password2" placeholder="Confirm password" required data-equal-to="#txtPassword" />

    <input type="submit" value="register" />
</form>

Now, once the form is submitted, the validation plugin will be called and your form is being validated before it is submitted. Keep in mind, that you don't need to add the novalidate attribute to turn off browser validation - it will be added automatically once the plugin is initialized.

See it live on JSFiddle

Validation Features

Checking for empty values

Add the required attribute to a field to make the plugin check if a value is filled in.

<input type="text" name="username" required>

If you don't like to use the required attribute, you can add the js-required class to your field also.

<input type="text" name="username" class="js-required">

Don't like the class either, or integrating with existing code? You can override the selector for looking for required fields by adding the data-required-selector attribute to your form, putting any jQuery selector there.

<form action="..." method="post" data-validate data-required-selector=".input-text_required">
  <input type="text" name="username" class="input-text input-text_required">
  <input type="submit" value="send">
</form>

Validation based on patterns

Out of the box, the plugin allows you to validate a field value based on a pattern. There are two pre-configured patterns: email and url. You can also define any number of custom patterns.

Email validation

The built-in validation pattern for emails is using the following regex:

/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zа-яёії\-0-9]+\.)+[a-zа-яёії]{2,}))$/i

To make it work, simply set the type of your input to email.

<!--
  A required email field, first will be checked for
  an empty value, then validated based on pattern
-->
<input type="email" name="email" required>

<!--
  An optional email field, empty values are allowed,
  pattern validation triggered when a value is entered.
-->
<input type="email" name="optional_email">
Url validation

The built-in validation pattern for urls is using the following regex:

/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/i

To make it work, simply set the type of your input to url.

<!--
  A required url field, first will be checked for
  an empty value, then validated based on pattern
-->
<input type="url" name="link" required>

<!--
  An optional url field, empty values are allowed,
  pattern validation triggered when a value is entered.
-->
<input type="url" name="optional_link">

Input length validation

Sometimes you need to validate the length of the string the user has filled in a field. Use the minlength and maxlength attributes to achieve the correct behavior. Should you set the maxlength attribute, the library will also take care of that the user cannot input more characters than required:

<!--
  A required field, you have to enter at east 3 characters
  and cannot enter more than 10 characters
-->
<input type="text" name="username" minlength="3" maxlength="10" required>

See it live on JSFiddle

Validating min and max values of numeric fields

Should you have a numeric field, mostly two things are required - limiting the characters to numeric only, as well as setting the minimum and maximum allowed values. Set the inputmode attribute of your field to numeric and use the min and max attributes to limit the number range. Should the user input a value below or above the given limits, the plugin will automatically correct the entered value.

<!--
  A required numeric field, accepts numbers from 18 to 100
-->
<input type="text" name="age" inputmode="numeric" min="18" max="100" required>

See it live on JSFiddle

Linking fields to check for equal values

A common practice in registration forms is to ask the user to input his password twice and validate if the values in both password fields match. The plugin can help you to easily link two fields with each other using the data-equal-to attribute, which accepts a selector as its value:

<!--
  Two password fields linked to each other and
  validated to have matching values
-->
<input id="txtPassword" type="password" name="password" placeholder="Password" required />
<input type="password" name="password2" placeholder="Confirm password" required data-equal-to="#txtPassword" />

See it live on JSFiddle

Allowing empty values

You have a field you need be validated when the user has entered a value and still allow empty values? Use the data-allow-empty attribute.

<!--
  A required email field, which is only checked
  when the user enters a value
-->
<input type="email" name="email" placeholder="Email" required data-allow-empty>

See it live on JSFiddle