1.2.0 • Published 6 years ago

input-basic-validator v1.2.0

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
6 years ago

input-basic-validator.js

npm version

A string basic validator

Strings only

This module validates strings only.

Installation and Usage

Server-side usage

npm install input-basic-validator

const iv = require('input-basic-validator');

iv.isEmpty(''); // => true

Import only a subset of the module:

const isEmpty = require('input-basic-validator/lib/isEmpty');
isEmpty('hello world'); // => false
// Or
const isEmpty = require('input-basic-validator').isEmpty;
isEmpty('hello world'); // => false

Validators

**isEmpty(str, options)**

Check if the given string is empty(has length of zero). options default value: { ignore_space: false } if ignore_space is set to true, spaces will be ignored Example:

isEmpty('    ', { ignore_space: true }); // => true
isEmpty('    ', { ignore_space: false }); // => false

**isNumeric(str, options)**

Check if the given string contains only digits and valid symbols. options default value: { no_symbols: false } If no_symbols set to true, the symbols '+', '-', '.' will be rejected. Example:

isNumeric('+123.456', { no_symbols: false }); // => true
isNumeric('+123.456', { no_symbols: true }); // => false

**isInt(str, options)**

Check if the given string is integer. options can contain the following flags:

  • lt:  to check if the value less than.
isInt('10', { lt: 11 }); // => true
isInt('11', { lt: 11 }); // => false
  • gt:  to check if the value greater than.
isInt('12', { gt: 11 }); // => true
isInt('11', { gt: 11 }); // => false
  • lte:  to check if the value less than or equal.
isInt('11', { lte: 11 }); // => true
isInt('12', { lte: 11 }); // => false
  • gte:  to check if the value greater than or equal.
isInt('11', { gte: 11 }); // => true
isInt('10', { gte: 11 }); // => false

**isEmail(str, options)**

Check if the given string is email. options default value: { ignore_max_length: false } max_length equal to 254. If ignore_max_length set to true the email can contain more than max_length characters. Example:

isEmail('foo@bar.com'); // => true
isEmail('foobar.com'); // => false

**contains(a, b, options)**

Check if a contains b. options default value: { case_sensitive: true } If case_sensitive set to false then the evaluation will ignore the case of the given strings. Example:

contains('hello world', 'llo'); // => true
contains('HELLO WORLD', 'llo', { case_sensitive: false }); // => true
contains('HELLO WORLD', 'llo', { case_sensitive: true }); // => false

**equals(a, b, options)**

Check if a match b. options default value: { case_sensitive: true } If case_sensitive set to false then the evaluation will ignore the case of the given strings. Example:

equals('abc', 'abc'); // => true
equals('abc', 'ABC', { case_sensitive: false }); // => true
equals('abc', 'ABC', { case_sensitive: true }); // => false

**matches(str, pattern, modfiers)**

Check if str matche pattern. Example:

matches('foo', '^[a-z]+$', 'i'); // => true
matches('FOO', /^[a-z]+$/i); // => true
1.2.0

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago