Nascent Personality Manifestation
Have opinions about JavaScript? We want to hear them. Take the 2018 JavaScript Ecosystem Survey »

@yokotak0527/kensho

1.2.1 • Public • Published

Kensho

The JavaScript form validation library.

Build Status codecov MIT licence

Feature

  • No dependency libraries.
  • Support 2byte charctors.
  • It can be added your validation rules, plugin and hooks.
  • It can be checked form has invalid values.
Browsers
Chrome
FireFox
Safari
IE11
Edge

Install

$ npm install @yokotak0527/kensho

How to use

var kensho = new Kensho('#my-form');
kensho.add(
    'input[name=name]',
    'p.error-msg',
    {
        'required' : 'this is required.',
    },
    'keyup|blur'
);

with validate options

var kensho = new Kensho('#my-form');
kensho.add(
    'input[name=name]',
    'p.error-msg',
    {
        'age' : {
            'errorMessage' : 'under 20 only.',
            'param' : {
                'maxAge' : 20
            }
        },
    },
    'keyup|blur'
);

or you can write more simply with Array.

var kensho = new Kensho('#my-form');
kensho.add(
    'input[name=name]',
    'p.error-msg',
    {
        'age' : [
            'under 20 only.',
            { 'maxAge' : 20 }
        ]
    },
    'keyup|blur'
);

manual validate

var kensho = new Kensho('#my-form');
kensho.add(
    'input[name=name]',
    'p.error-msg',
    {
        'required' : 'this is required.',
    },
    'keyup|blur'
);
kensho.validate('name'); // return boolean

validation at once

if you want to stop the form send event, when a form has invalid values.

var $send = document.querySelector('#send');
$send.addEventlistener('click', (e)=>{
    kensho.allValidate();
    if ( kensho.hasError() ) e.preventDefault();
});

static validate

Kensho.validate('email', 'a@a.com'); // return boolean

Default validation rules list

  1. required
  2. age
  3. range
  4. number
  5. email
  6. whitelist
  7. blacklist
  8. halfsize
  9. fullsize
  10. regexp

Change log

v1.2.0

  • added regexp rule.
  • fixed elem argument of all rules when val argument is a array.

v1.1.0

  • added HTMLElement array arugment in rule callback functions.
  • changed to support array in rule argument of kensho.add function.

install

npm i @yokotak0527/kensho

Downloadsweekly downloads

29

version

1.2.1

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability