• Stars
    star
    257
  • Rank 153,593 (Top 4 %)
  • Language
    JavaScript
  • Created over 11 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

HTML form validation. Perfectly made for all scenerios, lightweight, semantic & easy to use

Validator - Lightweight & Easy HTML form inputs checker

The Validator is cross-browser and will give you the power to use future-proof input types such as:
tel, email, number, date, time, checkbox and url.

👉 See Demo

npm i @yaireo/validator --save

// usage:

import FormValidator from '@yaireo/validator'

// or download the `validator.js` file and add load it inside your HTML file
<script src='validator.js'></script>

Why should you use this?

  • Cross browser validation
  • Deals with all sorts of edge cases
  • Utilize new HTML5 types for unsupported browsers
  • Flexible error messaging system
  • Light-weight (19kb + comments, unminified)

Validation types support

HTML5 offers a wide selection of input types. I saw no need to support them all, for example, a checkbox should not be validated as ‘required’ because why wouldn’t it be checked in the first place when the form is rendered?

For a full list of all the available types, visit the working draft page. These input types can be validated by the JS for – <input type='foo' name='bar' />. (Support is synthesized)

✔️ text
✔️ email
✔️ password - also comparing "re-type password" inputs
✔️ number
✔️ date
✔️ time
✔️ uRL
✔️ search
✔️ file
✔️ tel
✔️ checkbox
✔️ select
✔️ textarea
✔️ hidden – can also have the ‘required’ attribute

Basic semantics

<form action="" method="post" novalidate>
    <fieldset>
        <div class="field">
            <label>
                <span>Name</span>
                <input data-validate-length-range="6" data-validate-words="2" name="name" placeholder="ex. John f. Kennedy" required="required" type="text" />
            </label>
            <div class='tooltip help'>
                <span>?</span>
                <div class='content'>
                    <b></b>
                    <p>Name must be at least 2 words</p>
                </div>
            </div>
        </div>
        <div class="field">
            <label>
                <span>email</span>
                <input name="email" required="required" type="email" />
            </label>
        </div>
            ...

First, obviously, there is a Form element with the novalidate attribute to make sure all the native HTML5 validations (which currently suck) are disabled. Proceeding, there is a Fieldset element which is not a must, but acts as a “binding” box for a group of fields that are under the same “category”. For bigger forms there are many field groups that are visually separated from each other for example. Now, we treat every form field element the user interacts with, whatsoever, as a “field”, and therefor these “fields” will be wrapped with <div class='field'>. This isolation gives great powers. Next, inside a field, there will typically be an input or select or something of the sort, so they are put inside a <label> element, to get rid of the (annoying) 'For' attribute, on the label (which also requires us to give an ID to the form field element), and now when a user clicks on the label, the field will get focused. Great. Going back to the label’s text itself, we wrap it with a <span> to have control over it’s style.

The whole approach here is to define each form field (input, select, whatever) as much as possible with HTML5 attributes and also with custom attributes.

HTML Attributes on form elements

Attribute Purpose
required Defines that this field should be validated (with JS by my implementation and not via native HTML5 browser defaults)
placeholder Writes some placeholder text which usually describes the fields with some example input (not supported in IE8 and below)
pattern Defines a pattern which the field is evaluated with. Out-of-the-box available values are:
numeric - Allow only numbers
alphanumeric - Allow only numbers or letters. No special language characters
phone - Allow only numbers, spaces or dashes.

Alternatively, you may write your own custom regex here as well
data-validate-words Defines the minimum amount of words for this field
data-validate-length Defines the length allowed for the field (after trim). Example value: 7,11 (field can only have 7 or 11 characters). you can add how many allowed lengths you wish
data-validate-length-range Defines the minimum and/or maximum number of chars in the field (after trim). value can be 4,8 for example, or just 4 to set minimum chars only
data-validate-linked Defines the field which the current field value (the attribute is set on) should be compared to. Value can be a selector or another input's name attribute's value
data-validate-minmax For type number only. Defines the minimum and/or maximum value that can be in that field
data-validate-text-invalid Error text message for specific field

pattern attribute

It is possible to write your own unique Regex patterns directly in the attribute or reference to it by custom name, for example:

<input type="text" required="required" pattern="myCustomRegex" />
var validator = new FormValidator({
    regex : {
        myCustomRegex: '/whatever/'
    }
}, );

Optional fields

There is also support for optional fields, which are not validated, unless they have a value. The support for this feature is done by adding a class optional to a form element. Note that this should not be done along side the “required” attribute.

Error messages

{
    invalid         : 'input is not as expected',
    short           : 'input is too short',
    long            : 'input is too long',
    checked         : 'must be checked',
    empty           : 'please put something here',
    select          : 'Please select an option',
    number_min      : 'too low',
    number_max      : 'too high',
    url             : 'invalid URL',
    number          : 'not a number',
    email           : 'email address is invalid',
    email_repeat    : 'emails do not match',
    date            : 'invalid date',
    time            : 'invalid time',
    password_repeat : 'passwords do not match',
    no_match        : 'no match',
    complete        : 'input is not complete'
}

This object can be extended easily. The idea is to extend it with new keys which represent the name (attribute) of the field you want the message to be linked to, and that custom message appear as the general error one. Default messages can be over-ridden.

Example of a specific input field's error message:

<input type="text" data-validate-length-range="2,6" required="required" pattern="alphanumeric"  data-validate-text-invalid='Please follow the pattern rules'/>

Another example:

<input type="text" name="mySpecialInput" data-validate-length-range="2,6" required="required" pattern="alphanumeric" />
var validator = new FormValidator({
    texts : {
        mySpecialInput: 'wrong input' // set custom error message for that specific field, by "name"
    }
});

Example: for a given type date field, lets set a custom (general error) message like so:

// set custom text on initialization:
var validator = new FormValidator({
    texts : {
        date: 'not a real date'
    }
});

// or post-initialization
var validator = new FormValidator();
validator.texts.date = 'not a real date';

Error messages can be disabled:

    validator = new FormValidator({
        alerts: false
    });

    // or by:
    var validator = new FormValidator();
    validator.settings.alerts = false;

Binding the validation to a form

There are two ways to validate form fields, one is on the submit event of the form itself, then all the fields are evaluated one by one. The other method is by binding the checkField function itself to each field, for events like Blur, Change or whatever event you wish (I prefer on Blur).

Usage example - validate on submit

A generic callback function to have the form validated on the Submit event. You can also include your own personal validations before the checkAll() call.

var validator = new FormValidator();
// select your "form" element from the DOM and attach an "onsubmit" event handler to it:
document.forms[0].onsubmit = function(e){
    var validatorResult = validator.checkAll(this); // "this" reffers to the currently submitetd form element

    return !!validatorResult.valid;
};

Usage example - validate on field blur/input/change events

Check every field (using event Capture)

var validator = new FormValidator();

document.forms[0].addEventListener('blur', function(e){
    validator.checkField(e.target)
}, true);

document.forms[0].addEventListener('input', function(e){
    validator.checkField(e.target);
}, true);

document.forms[0].addEventListener('change', function(e){
    validator.checkField(e.target)
}, true);

Utilize the internal events' binding system; pass in the settings the events property and assign it an array which states which events should be inputs be validated for. For a single events, a string may be paassed instead of an Array:

var validator = new FormValidator( document.forms[0], {
    "events" : ['blur', 'input', 'change'] // for a single one, just pass a string, ex: "blur"
});

In case the form is not yet ready, the events maybe be binded later, when the form is ready, using the internal events method for a validator instance:

// validate fields on these events:

var validator = new FormValidator(document.forms[0]); // the "<form>" element should be passed when the instance is created or passed to the "events" method below (as the 2nd parameter)

// wait for the form, or its' fields, to be ready (for whatever reason), and then bind the events as follows:
validator.events(['blur', 'input', 'change']);

Tooltips (for each field which did not validate)

The helper tooltips <div class='tooltip help'>, which work using pure CSS, are elements which hold a small '?' icon and when hovered over with the mouse, reveal a text explaining what the field “field” is about or for example, what the allowed input format is.

Classes

validator.settings.classes object can be modified:

var validatorClasses = {
    field : 'field', // class for each input wrapper
    alert : 'alert', // call on the alert tooltip
    bad   : 'bad'    // classes for bad input
};

validator = new FormValidator(null, {classes:validatorClasses});

// or
validator = new FormValidator();
validator.settings.classes.bad = 'error';

Bonus – multifields

I have a cool feature I wrote which I call "multifields". These are fields which are often used to input a credit card or a serial number, and are actually a bunch of input fields which are “connected” to each other, and treated as one. You can see it in the demo page, and it’s included in the ‘multifield.js’ file.

More Repositories

1

tagify

🔖 lightweight, efficient Tags input component in Vanilla JS / React / Angular / Vue
HTML
3,222
star
2

fancyInput

Makes typing in input fields fun with CSS3 effects
CSS
1,928
star
3

photobox

A lightweight CSS3 image viewer that is pretty to look and and easy to use
JavaScript
742
star
4

fakescroll

vanilla-js lightweight custom HTML scrollbar
JavaScript
360
star
5

knobs

UI knobs controllers for JS/CSS live manipulation of various parameters
JavaScript
273
star
6

pathAnimator

Moves a DOM element along an SVG path (or do whatever along a path...)
HTML
264
star
7

stickyfloat

This plugin makes it possible to have a fixed position element that is relative to it’s parent. A normal fixed positioned element would be “out of context” and is very difficult to use in the most common situations with fluid designs. This plugin solves that problem with as little code as I could possible get it so it will run in the most optimized way, while also allow you to customize it in many important ways which might suit you best.
CSS
167
star
8

ui-range

Beautiful UI-Range input component, highly customisable, based on CSS variables. including a floating output value, min & max values on the sides & ticks according to the steps
SCSS
92
star
9

console-colors

Browser Console logs with colors
JavaScript
78
star
10

color-picker

Minimal javascript color picker component
JavaScript
53
star
11

relative-time

javascript function to transform timestamp or date to local relative-time
JavaScript
36
star
12

Do-in

Run tasks repeatedly in a fixed duration
HTML
23
star
13

dragsort

Animated drag sorting for horizontal list items
JavaScript
20
star
14

letterer

breaks a DOM node text into separate letters
HTML
16
star
15

touchy

JavaScript
16
star
16

dateRangePicker

pick a range between dates (months) or from presets
JavaScript
13
star
17

hover-carousel

mouse-position relative carousel
JavaScript
12
star
18

jqSwipe

Minimal swipe special event for touch-enabled devices
JavaScript
11
star
19

title-tooltip

Automatically converts HTML title attributes to nicer better tooltips
JavaScript
10
star
20

simpleGrid

the most performant grid system for same-size items
JavaScript
10
star
21

infinite

smart endless scrolling
JavaScript
9
star
22

listBreaker

split HTML lists into lists chunks
JavaScript
8
star
23

react-bouncer

Delay rendering of a React component with Debounce, Throttle, or any custom method
8
star
24

react-number-input

Renders native number input as a locale number (on blur) and reverts back to a workable number (on focus)
JavaScript
7
star
25

position

Position a DOM element at a certain X,Y or next to another element
JavaScript
6
star
26

react-ref-watcher

Watchable refs changes with conditional re-renders
JavaScript
6
star
27

input_indicator

JavaScript
5
star
28

ui-switch

React Switch component based on native checkbox input, using CSS variables (custom properties) as much as possible. Easily customisable and super lightweight.
JavaScript
4
star
29

carouselite

basic, light-weight Carousel jQuery plugin
JavaScript
3
star
30

react-hooks

React Hooks authored by myself
JavaScript
2
star
31

javascript-interview-questions

javascript interview questions
2
star
32

endless

infinite scrolling jQuery plugin
JavaScript
2
star
33

Mini-page-navigation

lets the user navigate through a web page using a "mini-page" helper on the side of the page, so the user could see where he is in relation to page as a whole, (showing the actual page as a small canvas image)
2
star
34

password-unveil

Unveil password input fields on mouse hover
JavaScript
2
star
35

gulp-file-contents-to-keys

convert files into key/value objects where key is filename, value is the content
JavaScript
2
star
36

translation-yaml-manager

A UI-only system which intends to ease the process of managing YAML translations files across multiple languages
1
star
37

gulp-template-compile-es6

Compile template files to ES6 exported functions
JavaScript
1
star
38

saSlider

Super Awesome Slider - jQuery plugin
CSS
1
star
39

sb-issue-performance-essentials-addon

issue report for Storybook slow rendering
JavaScript
1
star
40

multi-range-slider

Custom input element for multiple sliders in one range
JavaScript
1
star
41

self-descriptive-numbers

Generates self-descriptive numbers
HTML
1
star
42

sb-issue--react-client

Strobook issue #17926
JavaScript
1
star
43

ToDo

JavaScript
1
star
44

jQuery-ajax-delay

Wrapper for jQuery AJAX method which sets a random delay which simulates real-world latency
JavaScript
1
star
45

swc-js-extension-fails-with-jsx-content

For SWC bug report
JavaScript
1
star