Minimal HTML5 Form Validation Library – okjs

Category: Form , Javascript | September 21, 2018
Author: FelixRilling
Views Total: 2,876
Official Page: Go to website
Last Update: September 21, 2018
License: MIT

Preview:

Minimal HTML5 Form Validation Library – okjs

Description:

okjs is a JavaScript library used to extend the HTML5 form validation api that supports custom field validation rules & error messages. Without any 3rd dependencies.

How to use it:

Load the main JavaScript file ok.js when needed.

<script src="ok.js"></script>

Initialize the form validation library and add your own validation rules and error messages.

Ok({
  el: "form",
  validators: {
    nameCaps: {
      msg: "Please input your name in caps",
      fn: val => {
          return /[A-Z ]+/.test(val);
      }
    },
    emailDe: {
      msg: "Please input your .com email",
      fn: val => {
          return val.endsWith(".com");
      }
    }
  }
});

Apply the validation rules to the form fields using HTML data attributes as shown below:

<input type="text" placeholder="Enter Name" required data-ok="nameCaps">
<input type="email" placeholder="Enter email" required data-ok="emailDe">

Changelog:

v3.4.0 (09/21/2018)

  • feat: Added support for legacy browsers not supporting the validation API

You Might Be Interested In:


Leave a Reply