Giter Club home page Giter Club logo

ok's Introduction

Ok.js

A tiny JavaScript library for form validation.

Introduction

Ok.js is a utility library to validate forms with more than what HTML5 offers you. Features include customized messages and validator chaining.

Documentation

Usage

Install the package:

npm install okjs --save

Choosing an API

Ok.js offers two different APIs to interact with it:

  • The data-attribute API works via HTML data-attributes and works well without any frontend framework.
  • The composition API uses a more functional approach and is useful when combined with a frontend framework such as Vue or React.

The two offer the same functionality, but depending on your use-case, one may be preferable.

Data-Attribute API

The data-attribute API works well when working directly with HTML that is enhanced by JS.

import { Ok } from "okjs";

/**
 * Create Ok instance with custom validators.
 */
const ok = new Ok({
	/**
	 * A validator with the name 'nameFirst'.
	 */
	nameFirst: {
		msg: "Only 'Dave' allowed",
		/**
		 * Validation passes if the element value is "Dave".
		 */
		fn: (element) => element.value === "Dave",
	},
	emailDe: {
		msg: (element) =>
			`Please input your .de email (You entered '${element.value}')`,
		fn: (element) => element.value.endsWith(".de"),
	},
});

/**
 * Bind validation event handlers to inputs.
 */
document.querySelectorAll("[data-ok]").forEach((el) => {
	el.addEventListener("input", (e) => ok.validate(e.target));
});

The validator which will be used is defined in the DOM via data-ok data-attributes:

<form>
	<label for="demo1_1">First Name (only "Dave" allowed)</label>
	<input id="demo1_1" type="text" required data-ok="nameFirst" />

	<label for="demo1_2">Last Name (not validated, anything goes)</label>
	<input id="demo1_2" type="text" required />

	<label for="demo1_3">Email address (only ".de" allowed)</label>
	<input id="demo1_3" type="email" required data-ok="emailDe" />

	<input type="submit" />
</form>

The value defined in data-ok is the key of the validator dictionary object provided in the JavaScript Ok.js constructor parameter. If the given function evaluates to false, the input will be marked as invalid, and the message will be shown.

Composition API

The composition API works well if you already use a frontend framework like React or Vue, and do not want to use data attributes for logic.

Example using React:

import { validate } from "okjs";

/**
 * A validator with the name 'nameFirst'.
 */
const nameFirst = {
	msg: "Only 'Dave' allowed",
	fn: (element) => element.value === "Dave",
};

const emailDe = {
	msg: (element) =>
		`Please input your .de email (You entered '${element.value}')`,
	fn: (element) => element.value.endsWith(".de"),
};

const SomeComponent = () => {
	const handleNameChange = (e) => {
		/**
		 * Trigger validation using the 'nameFirst' validator.
		 */
		validate(e.target, [nameFirst]);
	};
	const handleEmailChange = (e) => {
		validate(e.target, [emailDe]);
	};

	return (
		<form>
			<label htmlFor="demo2_1">First Name (Only 'Dave' allowed)</label>
			<input id="demo2_1" type="text" onChange={handleNameChange} />

			<label htmlFor="demo2_2">
				Last Name (not validated, anything goes)
			</label>
			<input id="demo2_2" type="text" required />

			<label htmlFor="demo2_3">Email address (only ".de" allowed)</label>
			<input
				id="demo2_3"
				type="email"
				required
				onChange={handleEmailChange}
			/>

			<input type="submit" />
		</form>
	);
};

Chaining

Multiple validators can be used for a single element in a given order by chaining them. To chain multiple validators, add a comma between their names in the data-attribute. When using chaining, the field will only be considered valid if all validators succeed. Once a validator marks the field as invalid, all further validators are skipped.

<label for="demo2_1">Email ID (all caps and ending in .de)</label>
<input id="demo2_1" type="email" required data-ok="nameCaps,emailDe" />

In the composition API, the additional validators can be specified when calling validate:

validate(e.target, [nameCaps, emailDe]);

Localization

While Ok.js itself does not localize validation messages, the validation message function can call an existing localization function:

import { localize } from "./some-localization-tool";

const nameFirst = {
	msg: () => localize("validation.error.daveOnly"),
	fn: (element) => element.value === "Dave",
};

Caveats

Radiobuttons

Due to the way input elements with type radio work, validation is not straightforward. A single radio button only fires its change/input event when itself is changed, not if it becomes inactive due to another radiobutton being selected. A workaround is using an enclosing fieldset and listening to the events of that.

Compatibility

Ok.js should work in all browsers that support the following:

ok's People

Contributors

felixrilling avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ok's Issues

Fix v4.x typings

Validator generic type is currently broken when specified explicitely

Why only cutting edge browser support?

This is a very good library but I thing it could be writing to support even ie9
Its solve one problem well but i thing lack of support for ie9 and even ie10 presents a drawback
Can you rework on this?

Need to use multiple validations at once

Great library, a really lightweight interface to attach user validations to form fields. But what if I need to use more than one validation. For example, I have an IP address text field and I need to check it for presence (i.e. check if it is not empty) AND check the IP address format? I know: I can just write my own validator to account both of that conditions but is there any way to do this smarter?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.