input-is
Version:
Simple input validation
254 lines (168 loc) • 4.95 kB
Markdown
[](https://travis-ci.org/aichbauer/node-input-is)
[](https://coveralls.io/github/aichbauer/node-input-is?branch=master)
> Simple input validation
Almost any project needs a form validation.
This package simplifies your workflow.
```sh
npm i -S input-is
```
or
```sh
yarn add input-is
```
After installation you can import the package to your project and use a declarative way to validate your form inputs.
The return value is always a `boolean`, e.g. `true` or `false`.
Here is a litte example:
```js
import inputIs from 'input-is'; // const inputIs = require('input-is');
inputIs.email('hello'); // false
inputIs.email('example@mail.com'); // true
```
**Functions:**
- [date](
- [datetime](
- [email](
- [exactly](
- [filled](
- [float](
- [integer](
- [max](
- [min](
- [not](
- [number](
- [partOf](
- [phonenumber](
- [time](
- [url](
- [valid](
> Note: you have to pass the format of the `date` as second parameter to the function
This function validates if a form input is a valid date
**return value:** boolean
```js
// valid formats:
// YYYY-MM-DD, MM-DD-YYYY,
// DD-MM-YYYY, YYYY/MM/DD,
// MM/DD/YYYY, DD/MM/YYYY,
inputIs.date('100/100/1000'); // false
inputIs.date('12/12/2017', 'DD/MM/YYYY'); // true
```
> Note: you have to pass the format of the `date` as second parameter to the function
This function validates if a form input is a valid datetime
**return value:** boolean
```js
// valid formats:
// YYYY-MM-DD hh:mm:ss, YYYY-MM-DD hh:mm,
// MM-DD-YYYY hh:mm:ss, MM-DD-YYYY hh:mm,
// DD-MM-YYYY hh:mm:ss, DD-MM-YYYY hh:mm,
// YYYY/MM/DD hh:mm:ss, YYYY/MM/DD hh:mm,
// MM/DD/YYYY hh:mm:ss, MM/DD/YYYY hh:mm,
// DD/MM/YYYY hh:mm:ss, DD/MM/YYYY hh:mm,
inputIs.datetime('12/12/2017'); // false
inputIs.datetime('12/12/2017 12:12:12', 'DD/MM/YYYY'); // true
```
This function validates if a form input is a valid email
**return value:** boolean
```js
inputIs.email('example.mail.com'); // false
inputIs.email('example@mail.com'); // true
```
This function validates if a form input is a exactly the same as a target
**return value:** boolean
```js
inputIs.exactly('this should be', 'exactly like this target'); // false
inputIs.exactly('exactly the same', 'exactly the same'); // true
```
This function validates if a form input is filled, e.g. the value is minimum one character long
**return value:** boolean
```js
inputIs.filled(''); // false
inputIs.filled('1'); // true
```
This function validates if a form input is a valid float (not type number)
**return value:** boolean
```js
inputIs.float('1'); // false
inputIs.float('1.0'); // true
```
This function validates if a form input is a valid integer (not type number)
**return value:** boolean
```js
inputIs.integer('z'); // false
inputIs.integer('990'); // true
```
This function validates if a form input is maximum the length of target
**return value:** boolean
```js
inputIs.max('value', 3); // false
inputIs.max('value', 5); // true
```
This function validates if a form input is minimum the length of a target
**return value:** boolean
```js
inputIs.min('value', 6); // false
inputIs.('value', 3); // true
```
This function validates if a form input is not like the target
**return value:** boolean
```js
inputIs.not('target', 'target'); // false
inputIs.not('value', 'target'); // true
```
This function validates if a form input is a valid number (not type number)
**return value:** boolean
```js
inputIs.number('number'); // false
inputIs.number('1'); // true
```
This function validates if a form input is part of a provided characterset
**return value:** boolean
```js
inputIs.partOf('z', 'part of ABC'); // false
inputIs.partOf('ABC', 'part of ABC'); // true
```
This function validates if a form input is a valid phonenumber
**return value:** boolean
```js
inputIs.phonenumber('+12'); // false
inputIs.phonenumber('+1234567890'); // true
```
This function validates if a form input is a valid time
**return value:** boolean
```js
inputIs.time('01:'); // false
inputIs.time('01:01'); // true
```
This function validates if a form input is a valid url
**return value:** boolean
```js
inputIs.url('google.'); // false
inputIs.url('https://www.google.com'); // true
```
This function validates form input to a regular expression
**return value:** boolean
```js
inputIs.valid('google', /.at/); // false
inputIs.valid('hat', /.at/); // true
```
MIT © Lukas Aichbauer