All Projects → rtivital → validate

rtivital / validate

Licence: other
Modern lightweight library without dependencies for the data validation from single input tag

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to validate

oolong
oolong 🍵 : create and administrate validation tests for typical automated content analysis tools.
Stars: ✭ 40 (+66.67%)
Mutual labels:  validation
validator
💯Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving
Stars: ✭ 9,721 (+40404.17%)
Mutual labels:  validation
ruby-magic
Simple interface to libmagic for Ruby Programming Language
Stars: ✭ 23 (-4.17%)
Mutual labels:  validation
validation
Validation on Laravel 5.X|6.X|7.X|8.X
Stars: ✭ 26 (+8.33%)
Mutual labels:  validation
openapi-schemas
JSON Schemas for every version of the OpenAPI Specification
Stars: ✭ 22 (-8.33%)
Mutual labels:  validation
fefe
Validate, sanitize and transform values with proper TypeScript types and zero dependencies.
Stars: ✭ 34 (+41.67%)
Mutual labels:  validation
onixcheck
ONIX validation library and commandline tool
Stars: ✭ 20 (-16.67%)
Mutual labels:  validation
strickland
Strickland is a JavaScript validation framework with a focus on extensibility and composition
Stars: ✭ 16 (-33.33%)
Mutual labels:  validation
webargs-starlette
Declarative request parsing and validation for Starlette with webargs
Stars: ✭ 36 (+50%)
Mutual labels:  validation
nexus-validate
🔑 Add argument validation to your GraphQL Nexus API.
Stars: ✭ 29 (+20.83%)
Mutual labels:  validation
excel validator
Python script to validate data in Excel files
Stars: ✭ 14 (-41.67%)
Mutual labels:  validation
xrechnung-schematron
Schematron rules for the German CIUS (XRechnung) of EN16931:2017
Stars: ✭ 19 (-20.83%)
Mutual labels:  validation
flask-pydantic
flask extension for integration with the awesome pydantic package
Stars: ✭ 181 (+654.17%)
Mutual labels:  validation
Swiftz-Validation
A data structure for validations. It implements the applicative functor interface
Stars: ✭ 15 (-37.5%)
Mutual labels:  validation
validada
Another library for defensive data analysis.
Stars: ✭ 29 (+20.83%)
Mutual labels:  validation
svelte-form
JSON Schema form for Svelte v3
Stars: ✭ 47 (+95.83%)
Mutual labels:  validation
ttv
A command line tool for splitting files into test, train, and validation sets.
Stars: ✭ 38 (+58.33%)
Mutual labels:  validation
js-form-validator
Javascript form validation. Pure JS. No jQuery
Stars: ✭ 38 (+58.33%)
Mutual labels:  validation
is-valid-domain
Validate domain name in JavaScript
Stars: ✭ 50 (+108.33%)
Mutual labels:  validation
apple-receipt
Apple InAppPurchase Receipt - Models, Parser, Validator
Stars: ✭ 25 (+4.17%)
Mutual labels:  validation

Input Validator

Input Validator is a modern lightweight library without dependencies for the data validation from single <input /> tag.

View demo here

Installation

Install with Bower:

bower install input-validator.js

Or clone from Github:

git clone https://github.com/rtivital/validate.git

Include library before closing <body> tag

<body>
	...
	<script src="input-validator.min.js"></script>
</body>

Grab the <input /> tag from the DOM tree and start the data validation

var emailInput = new Validator.init(document.getElementById('email'), {
  rules: {
    min: 5,
    max: 20,
    match: 'email'
  },
  onError: function() {
    var parentNode = this.element.parentNode;
    parentNode.classList.add('has-error');
    parentNode.classList.remove('has-success');
    parentNode.querySelector('.help-block').textContent = 'Error: ' + this.message;
  },
  onSuccess: function() {
    var parentNode = this.element.parentNode;
    parentNode.classList.add('has-success');
    parentNode.classList.remove('has-error');
    parentNode.querySelector('.help-block').textContent = 'Everything is valid!';
  }
});

Documentation

Configuration object

Validator.init function accepts consfiguration object like this one:

{
  rules: {
    min: 5,
    max: 20,
    match: 'email'
  },
  onError: function() {
    console.log('Error' + this.message);
  },
  onSuccess: function() {
    console.log('Everything is valid!');
  }
}

These are minimum set of values. Additionally you can pass in messages and regExps objects to replace predefined settings or add your own testing rules:

// Defaults:
{
  regExps: {
    email: /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i,
    url: /^((https?):\/\/(\w+:{0,1}\w*@)?(\S+)|)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/,
    numbers: /^\d+(\.\d{1,2})?$/,
    digit: /[0-9]*$/,
    letters: /[a-z][A-Z]*$/
  },
  messages = {
    required: 'This field is required',
    min: 'This field should contain at least %rule% characters',
    max: 'This field should not contain more than %rule% characters',
    match: 'This field shold countain a valid %rule%'
  }
}

Basic validation

Generally, you want the validation to occur with certain events (e.g. click, keyup, etc.). In this case consider this code:

var onError = function() {
  var parentNode = this.element.parentNode;
  parentNode.classList.add('has-error');
  parentNode.classList.remove('has-success');
  parentNode.querySelector('.help-block').textContent = 'Ошибка: ' + this.message;
};

var onSuccess = function() {
  var parentNode = this.element.parentNode;
  parentNode.classList.add('has-success');
  parentNode.classList.remove('has-error');
  parentNode.querySelector('.help-block').textContent = 'Ура! Всё прошло хорошо, ваши данные полность валидные.';
};

var emailInput = new Validator.init(document.getElementById('email'), {
  rules: {
    min: 5,
    max: 20,
    match: 'email'
  }
  onError: onError,
  onSuccess: onSuccess
});

document.getElementById('validate-btn').addEventListener('click', function(e) {
  e.preventDefault();
  emailInput.validate();
});

Validation methods

required

Validator.fn.required returns if the value contains at least one symbol, except for whitespace

validate(document.getElementById('email'), {
  rules: {
    required: true
  }
});

min

Validator.fn.min returns if the value length is more than provided length

validate(document.getElementById('email'), {
  rules: {
    min: 8
  }
});

max

Validator.fn.max returns if the value length is less than provided length

validate(document.getElementById('email'), {
  rules: {
    max: 20
  }
});

match

Validator.fn.match returns if the value matches certain pattern. Available patterns:

  • email
  • url
  • numbers
  • digits
  • letters
validate(document.getElementById('email'), {
  rules: {
    match: 'email'
  }
});

You can also define tou regular expression in config object and then use it:

validate(document.getElementById('email'), {
  regExps: {
    base64: /[^a-zA-Z0-9\/\+=]/i
  },
  rules: {
    match: 'base64'
  }
});

Callbacks

onError

onError callback, which you define in config object, will be called every time Validator.fn.validate method fails:

validate(document.getElementById('email'), {
  rules: {
    min: 8,
    max: 50,
    match: 'email'
  },
  onError: function() {
    console.log('Error: ' + this.message);
  }
});

onSuccess

onSuccess callback, which you define in config object, will be called every time Validator.fn.validate method passed:

validate(document.getElementById('email'), {
  rules: {
    min: 8,
    max: 50,
    match: 'email'
  },
  onSuccess: function() {
    console.log('Everything is valid');
  }
});

Creating messages

You can create your own messages with simple template variables %rule% and %data%:

validate(document.getElementById('email'), {
  rules: {
    min: 8,
    max: 50,
    match: 'email'
  },
  messages: {
    min: 'The value of this field shold be at least %rule% characters long. Value %data% does\'t fit well!',
    max: 'The value of this field shold not be longer than %rule% characters. Value %data% does\'t fit well!',
    match: '%data% is not a valid %rule% address'
  }
});

%data% refers to the current value from input field. %rule% refers to current param (e.g. 8 at min, 50 at max and email at match).

Browser support

Input validator uses Object.keys() ES5 feature.

Browser Support:

  • IE 9+
  • Chrome 23+
  • Firefox 21+
  • Opera 15+
  • Safari 6+
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].