Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/scriptex/dator

Frontend forms validator
https://github.com/scriptex/dator

form-validation form-validator

Last synced: 9 days ago
JSON representation

Frontend forms validator

Awesome Lists containing this project

README

        

[![Travis CI](https://travis-ci.com/scriptex/dator.svg?branch=master)](https://travis-ci.com/scriptex/dator)
[![Github Build](https://github.com/scriptex/dator/workflows/Build/badge.svg)](https://github.com/scriptex/dator/actions?query=workflow%3ABuild)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/34d3d75710534dc6a38c3584a1dcd068)](https://www.codacy.com/gh/scriptex/dator/dashboard?utm_source=github.com&utm_medium=referral&utm_content=scriptex/dator&utm_campaign=Badge_Grade)
[![Codebeat Badge](https://codebeat.co/badges/d765a4c8-2c0e-44f2-89c3-fa364fdc14e6)](https://codebeat.co/projects/github-com-scriptex-dator-master)
[![CodeFactor Badge](https://www.codefactor.io/repository/github/scriptex/dator/badge)](https://www.codefactor.io/repository/github/scriptex/dator)
[![DeepScan grade](https://deepscan.io/api/teams/3574/projects/5257/branches/40799/badge/grade.svg)](https://deepscan.io/dashboard#view=project&tid=3574&pid=5257&bid=40799)
[![Analytics](https://ga-beacon-361907.ew.r.appspot.com/UA-83446952-1/github.com/scriptex/dator/README.md?pixel)](https://github.com/scriptex/dator/)

# Dator. Vali-Dator

> Intuitive frontend forms validator based on HTML5 attributes and a tiny bit of JavaScript.

## Visitor stats

![GitHub stars](https://img.shields.io/github/stars/scriptex/dator?style=social)
![GitHub forks](https://img.shields.io/github/forks/scriptex/dator?style=social)
![GitHub watchers](https://img.shields.io/github/watchers/scriptex/dator?style=social)
![GitHub followers](https://img.shields.io/github/followers/scriptex?style=social)

## Code stats

![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/scriptex/dator)
![GitHub repo size](https://img.shields.io/github/repo-size/scriptex/dator?style=plastic)
![GitHub language count](https://img.shields.io/github/languages/count/scriptex/dator?style=plastic)
![GitHub top language](https://img.shields.io/github/languages/top/scriptex/dator?style=plastic)
![GitHub last commit](https://img.shields.io/github/last-commit/scriptex/dator?style=plastic)

## About

There are a lot of form validators out there. This one aims at intuitive API and relies on extensive usage of HTML data-\* attributes.
It's important to note that in order to use the validation, the `required` attribute must be present on your HTML element.

## Demo

[Here](https://dator.atanas.info) is a demo showing how to use the validator and all built-in validation types:

## Install

```sh
npm i dator

# or

yarn add dator
```

or include it from unpkg.com

```html

```

## Usage

In your JavaScript file:

```javascript
import { Validator } from 'dator';

const myForm = document.querySelector('form');
const datorSettings = {}; // see below for more details
const datorCustomTypes = {}; // see below for more details

const formValidator = new Dator(myForm, datorSettings, datorCustomTypes);
```

In your HTML file:

```html

```

The constuctor accepts three arguments:

1. DOM element for the form that needs to be validated (Required)
2. Object with settings (Optional) - more below
3. Object with custom validation types - more below

## Settings

```javascript
// These are the settings and their default values
const datorSettings = {
validClass: 'is--valid', // The CSS classname that will be added to a valid form element
errorClass: 'is--invalid', // The CSS classname that will be added to an invalid form element
validatedClass: 'is--validated', // The CSS classname that will be added to the validated form
watch: true, // If the validation should be applied "as-you-type"
classHolder: null, // The CSS classname of the element that should receive the `validClass` and `invalidClass`
beforeValidate: null, // A function to run before the actual form submission
onSubmit: null, // A function to run during the actual form submission
afterValidate: null // A function to run after the actual form submission
};
```

## Validation types

There are several built-in validation types:

- name
- zip
- presence
- email
- phone
- address
- integer
- float
- credit-card
- cvc

All of these validation types represent a regular expression which is used to test the value of the form element against to.

In order to add more validatior types, use the third constructor argument as shown above.

For example, if you need to add a new `zip` type:

```javascript
const datorCustomTypes = {
zip: /^\d{4}$/ // Change from 5 digits to 4 digits
};
```

## LICENSE

MIT

---


Connect with me:






 



 



 



 



 



 



 



 



 



 



 



---


Support and sponsor my work: