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

user-input-validation

v1.0.0

Published

Helper class that handles all the input validation and shows possible informative errors to the end user

Downloads

2

Readme

input validator

Previously part of utilities now it's it's own library. The purpose? To validate user input according to x or y rule and show any validation errors to the end user. This class uses utilities for the validation rules.

Instalation

You can either download (from index.js or /libs/) and include the script to your file: <script src="path/to/script"></script> manually or you can use ecmascript 6's require("input-validator")

In this repo there's also some CSS for the error handling bit that you can use by downloading the CSS and including it on your code ( <link rel="stylesheet" href="css/style.css"> ). Alternatively you can also make your own error handling / css thingy.

The error handling function adds a has-error to the parent element of input and creates a span element for the error message.

Usage

After including the file on your project all you need to do is validateInputs(validationObject, handleErrors);, where is an object (or array of objects in case of multiple field validation) with the element to validate and respective rule(s) (further explanation below) and handleErrors is a boolean that defaults to true that indicates whether the error messages are to be handled by this library or not.

validateInputs

This function validates DOM fields or simple values. Some of the rules also require a rule value, for instance when you using maxvalue you have to specify what's the max value. Every rule has it's own error message, however you can send your own custom error message (showed below). This function accepts either and object or an array of objects. This object contains the field / value to be validated, the rule, a custom message (optional), ruleValue (depends on the which rule we're applying) and optional (which is in itself optional)

Validation object composition:

{
  field: DOM element or value,
  rule: validation rule (string),
  message: a custom message you want for validation errors (string),
  ruleValue: some rules require a rulevalue (see below) 
  optional: for the times when a value is optional but must meet certain criteria (boolean)
}

Currently supports the following rules:

Examples

There are several ways for you to validate what you need. You can:

  • validate only one field with one validation rule
  • validate only one field with multiple validation rule
  • validate multiple fields with one validation rule each
  • validate multiple fields with multiple validation rule each
  • validate multiple fields with one validation rule for some fields and multiple for others You can also have a specific custom message for each validation rule

One field, one rule

validateInputs({
  input: value,
  rule: "required"
});

One field, multiple rules

validateInputs({
  input: value, 
  rule: ["required", "number"]
});

Multiple fields, one rule each

validateInputs([
  {input: value, rule: "required"},
  {input: value2, rule: "email"},
  {input: value3, rule: "even"}
]);

Multiple fields, multiple rules

validateInputs([
  {input: value, rule: ["required", "even"]},
  {input: value2, rule: ["email", "positive"]}
]);

Multiple fields, mixed rules

validateInputs([
  {input: value, rule: "required"},
  {input: value2, rule: ["email", "positive"]}
]);

Multiple rules with custom message each

validateInputs([
  {input: value, rule: "required"},
  {input: value2, rule: [
    {rule: "email", message: "Custom message"},
    {rule: "positive", message: "Custom message 2"} 
  ]}
]);

Rules with rule value

validateInputs([
  {input: value, rule: "maxvalue", ruleValue: 5},
  {input: value2, rule: "required", message: "something"},
  {input: value3, rule: "maxvalue", ruleValue: 5, message: "another something"},
  {input: value4, rule: [
    {rule: "minlen", message: "Custom message", ruleValue: 5},
    {rule: "positive", message: "Custom message 2"},
    {rule: "required"}
  ]}
]);

required

Checks if the value has something

validateInputs({input: value, rule: "required"});

number

Checks if the value is a number

validateInputs({input: value, rule: "number"});

even

Checks if the value is a number and if it's an even number

validateInputs({input: value, rule: "even"});

maxvalue

Checks if the value is lower than the given rule value

validateInputs({input: value, rule: "maxvalue", ruleValue: 5});

minvalue

Checks if the value is higher than the given rule value

validateInputs({input: value, rule: "minvalue", ruleValue: 5});

positive

Checks if the value is a number and if it's positive

validateInputs({input: value, rule: "maxvalue", ruleValue: 5});

equal

Checks if the value equals the value(s) from the rule value

validateInputs({input: value, rule: "equal", ruleValue: 5});

or

validateInputs({input: value, rule: "equal", ruleValue: [5, 2, 3]});

maxlen

Checks if the value length is lower than the given rule value

validateInputs({input: value, rule: "maxlen", ruleValue: 5});

minlen

Checks if the value length is higher than the given rule value

validateInputs({input: value, rule: "minlen", ruleValue: 5});

email

Checks if the value is a valid email

validateInputs({input: value, rule: "email"});

phone

Checks if the value is a valid phone number, according to the cirteria set by the rule value

validateInputs({input: value, rule: "phone", ruleValue: ["### ### ###", "#########"]});

or

validateInputs({input: value, rule: "phone", ruleValue: "#########"});