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

validation-form

v0.1.0

Published

Simple form validation library.

Downloads

1

Readme

Validation-Form

Simple form validation library. Validation rules are to be delivered by data-* attributes.

Requires

  • jquery
  • es5-shim (for legacy)
  • json2 (for legacy)

Basic Usage

HTML

<form id="my-form" method="post" action="post.php">
    <h3>Name:</h3>
    <input type="text" name="name" data-validation="required; length(0, 100)">
    <h3>Email:</h3>
    <input type="text" name="email" data-validation="required; email; length(0, 50)">
    <p>Please input again</p>
    <input type="text" name="email_confirm" data-validation="required; equals(email)">
    <input type="submit" value="SEND">
</form>

JavaScript

$("#my-form").validationForm({
    defaultMessage: "This field is required",
    messages: {
        name: {
            length: "Please input 100 characters or less"
        },
        email: {
            email: "Please input valid email address",
            length: "Please input 50 characters or less"
        },
        email_confirm: {
            equals: "Email addreses are not match"
        }
    }
});

Options

|Option|Type|Default|Description| |------|----|-------|----| |submit|Boolean|true|If false, prevent submit event when validated| |format|String|(read code)|Rule format string (RegExp)| |delimiter|String|";"|Delimiter which separates rules| |validateEvent|String|"change blur"|Events which trigger validation| |messages|Object|null|Messages which is shown when invalid| |messageClassName|String|"validation-message"|CSS class name for error message container| |messagePosition|String|"after"|Position on which message container is inserted (before, after)| |messageFade|Boolean|true|Fade-in/out error message or not| |messageDuration|Number|300|Duration time for fade-in/out error message|

Validation Methods

Apply validation methods by data-validation attribute as below.

<input name="name" ... data-validation="method; method(arg1, arg2); method(/regexparg/);">

|Method|Arguments|Description| |------|----|-------|----| |required|-|Check if it's not empty| |equals|name:String|Check if value equals to the named element's value| |length|min:Number, max:Number|Validate length of string| |range|min:Number, max:Number|Validate range of number| |email|-|Validate if value is valid email address| |url|-|Validate if value is valid url| |number|-|Validate if value is numeric string| |integer|-|Validate if value is integer| |pattern|pattern:RegExp|Validate value with pattern|

In the case that several elements are same named, such as checkbox or radio, data-validation attribute need only to be added to one of them.

<!-- input[type=text] -->
<input type="text" name="name" data-validation="required; pattern(/^\d+$/)">
<input type="text" name="age" data-validation="required; integer; range(20, 60)">

<!-- input[type=radio], input[type=checkbox] -->
<label><input type="checkbox" name="color" value="red" data-validation="required"> Red</label>
<label><input type="checkbox" name="color" value="green"> Green</label>
<label><input type="checkbox" name="color" value="blue"> Blue</label>

<!-- select -->
<select name="browser" data-validation="required">
    <option value="">--Please Select--</option>
    <option value="chrome">Google Chrome</option>
    <option value="firefox">Firefox</option>
    <option value="edge">Microsoft Edge</option>
    <option value="other">Other</option>
</select>

Filter Methods

Filter methods manupulate input's value before running validation methods. Apply filter methods by data-filter attributes as below.

<input name="name" ... data-filter="method; method(arg1, arg2); method(/regexparg/);">

|Method|Arguments|Description| |---|---|---| |trim|-|Trim string| |replace|pattern:RegExp(String), replacement:String|Replace pattern by replacement| |zenhan|-|Replace multibyte string by single one|

Other examples

Async submit

$("#my-form").validationForm({
    submit: false, // prevent submit event
    ...
})
.on("validated", function(){
    // when submitting form and all inputs are validated,
    // "validated" event triggered on form element
    $(this).submitAsync().done(function(){
        // do something by response
    });
});