• This repository has been archived on 10/Feb/2019
  • Stars
    star
    577
  • Rank 77,363 (Top 2 %)
  • Language
    JavaScript
  • Created about 14 years ago
  • Updated almost 6 years ago

Reviews

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

Repository Details

An HTML5 form validation plugin for jQuery. Works on all major browsers, both new and old. Implements inline, realtime validation best practices (based on surveys and usability studies). Developed for production use in e-commerce. Currently in production with millions of users.

h5Validate (WARNING -- DEPRECATED -- ARCHIVED)

Hi,

I'm Eric Elliott, author of "Programming JavaScript Applications" (O'Reilly). A few years ago, I wrote this jQuery plugin that understands HTML5 forms and knows how to validate them, even in browsers that don't yet support HTML5.

In browsers that do support HTML5, h5Validate adds some much-needed features, such as the ability to customize the user interface when an input fails validation.

Status

A lot has changed since I wrote this library in 2010. It has been several years since I used the library myself. I leave it here because other people might find it useful. There are some open issues, and I have no intention of updating or maintaining this library myself. This project is now and will always remain unmaintained. If you would like to use this code, please fork and maintain your own copy.

Documentation

Best practice realtime HTML5 form validation for jQuery. Works on all popular browsers, including old ones like IE6.

  • Regularly tested on 13 different browsers, IE6 - IE9, FireFox, Chrome, iPhone, and Android.
  • Implements best practices based on 1,000 user survey, several usability studies, and the behavior of millions of users in live production environments.

Supported Platforms

Desktop: IE 9, 8, 7, 6, Chrome, Firefox, Safari, and Opera. Tested on Windows 7 and Mac. Mobile: iPhone, Android, Palm WebOS

Jumpstart

Copy and paste this at the end of the body on any page with an HTML5 form. If html5 validation rules exist in the form, you're in business!

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="jquery.h5validate.js"></script>

<script>
$(document).ready(function () {
    $('form').h5Validate();
});
</script>

Features

1. HTML5 required attribute

E.g.:

<input id="name" name="name" type="text" placeholder="Bob" title="Your name is required." required>

2. HTML5 pattern attribute

E.g. This expects mm/dd/yyyy:

<input id="name" name="name" type="text" placeholder="mm/dd/yyyy" title="mm/dd/yyyy" pattern="(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d">

3. Pattern library

Sometimes, you'll want to re-use the same validation pattern for several elements on a page. You could copy and paste the same pattern over and over again to a bunch of different form fields, but what happens if you discover a bug in the pattern? Fix it and repeat the whole copy and paste process?

A better solution is to attach the pattern with a class selector. Use $.h5Validate.addPatterns() to add your pattern, make sure the classPrefix variable is set correctly (it's h5- by default), and add the class to the input fields.

The included patterns available are:

  • email
  • phone
  • email
  • url
  • number
  • dateISO
  • alpha
  • alphaNumeric
  • integer

E.g.:

<input type="text" class="h5-email" required />

4. Error messages

Error divs should be hidden in the HTML with display: none; -- h5Validate shows them when the corresponding field is marked invalid.

E.g.:

<form>
    <label for="FirstName">Your Name:*</label>
    <input id="FirstName" type="text" title="Required: Please provide your first name." required>
    <div id="invalid-FirstName" style="display: none;">
        This stuff will get replaced by the title contents.
    </div>
</form>

5. Options

There are a ton of options you can override by passing key: value pairs when you call .h5Validate(). For example, we can change the CSS class that indicates validation errors:

<head>
<style>
	.black {
		background-color:#111111;
		color:silver;
	}
</style>
</head>
<body>
<form id="black">
		<label for="name">Your Name:*</label>
		<input id="name" name="name" type="text" placeholder="Bob" title="Your name is required." required>
</form>

<script>
	$(document).ready(function () {
		$('#black').h5Validate({
			errorClass:'black'
		});
	});
</script>
</body>

errorClass

Custom CSS class for validation errors.

validClass

Custom CSS class to mark a field validated.

errorAttribute

An html attribute that stores the ID of the error message container for this element. It's set to data-h5-errorid by default. Note: The data- attribute prefix is a new feature of HTML5, used to store an element's meta-data.

E.g: <input id="name" data-h5-errorid="nameError" required >

kbSelectors

A list of CSS selectors for attaching keyboard-oriented events.

Default: kbSelectors: ':text, :password, select, textarea'

focusout, focusin, change, keyup

A list of CSS selectors for attaching "mouse oriented" events.

Default: mSelectors: ':radio, :checkbox, select, option'

click

(Event) The only default mouse-oriented event. Since it probably makes little sense to trigger validation on other mouse events, I'll leave it to you to figure out how to enable them.

Note: The click event isn't just for the mouse. It will trigger for keyboard and touch screen interactions, too.

click: true

6. Event API

h5Validate supports the following events:

instance

Instance created.

validated

The element in question has been validated. A validity object is passed into the event handler containing:

{
	element: HTMLObject, // A reference to the validated element
	customError: Bool, // Custom validity failed.
	patternMismatch: Bool, // Input does not match pattern
	rangeOverflow: Bool, // Input is greater than max attribute value
	rangeUnderflow: Bool, // Input is less than min attribute value
	stepMismatch: Bool, // Input does not conform to the step attribute setting
	tooLong: Bool, // Input is too long
	typeMismatch: Bool, // Wrong input type
	valid: Bool, // Input is valid
	valueMissing: Bool // Required value is missing
}

formValidated

The form in question has been validated. An object is passed with an object containing a bool, valid, and an array of validity objects corresponding to the validated elements.

7. Methods

$.h5Validate.addPatterns(patterns)

Take a map of pattern names and HTML5-compatible regular expressions, and add them to the patternLibrary. Patterns in the library are automatically assigned to HTML element pattern attributes for validation.

{object} patterns A map of pattern names and HTML5 compatible regular expressions.

E.g.:

<input class="h5-phone" id="phone" name="phone" type="text" placeholder="555-555-5555" title="555-555-5555" />

The class="h5-phone" bit is the part doing the magic. The h5- prefix tells you that this class is a handle that we can use to attach validation rules to. Internally, we just tack this prefix to the front of the pattern names to get the right selectors.

In your JavaScript, specify the pattern name without the class prefix. Keeping the prefix off lets us easily share and re-use pattern libraries between projects.

$(document).ready(function () {
	$.h5Validate.addPatterns({
		phone: /([\+][0-9]{1,3}([ \.\-])?)?([\(]{1}[0-9]{3}[\)])?([0-9A-Z \.\-]{1,32})((x|ext|extension)?[0-9]{1,4}?)/				
	});

	$(form).h5Validate();
});

$.h5Validate.validValues(selector, values)

Take a valid jQuery selector, and a list of valid values to validate against. If the user input isn't in the list, validation fails.

{String} selector Any valid jQuery selector.

{Array} values A list of valid values to validate selected fields against.

8. New input types


Warning: Each browser has its own way of treating these new input types. iOS might swap out the on-screen keyboard (cool!), while Chrome renders custom UI controls that don't always make sense (like up and down arrows for datetime inputs.)

What's worse, some of the styles that get applied to these elements are browser-specific, and ignore CSS cascading -- so before you can add your own look and feel, you first have to turn off each native browser's look and feel. For example, h5Validate works just fine on that search field down there, but in Chrome, it ignores our CSS because you first have to turn off -webkit-appearance: searchfield; before you style it. (Hint: You may want to search for a good HTML5 CSS reset).


h5Validate does not currently validate new element types automatically. The pattern and required attributes work fine, but h5Validate will not automatically apply email validation to fields with type="email", etc.. (Yet.)

Here are the HTML5 input types:

<form>
    <div><input title="normal" required></div>
    <div><input type="tel" required></div>
    <div><input type="search" required></div>
    <div><input type="email" required></div>
    <div><input type="url" required></div>
    <div><input type="datetime" required></div>
    <div><input type="date" required></div>
    <div><input type="month" required></div>
    <div><input type="week" required></div>
    <div><input type="time" required></div>
    <div><input type="datetime-local" required></div>
    <div><input type="number" required></div>
    <div><input type="range" required></div>
    <div><input type="color" required></div>
</form>

More Repositories

1

rtype

Intuitive structural type notation for JavaScript.
JavaScript
1,129
star
2

react-pure-component-starter

A pure component dev starter kit for React.
JavaScript
792
star
3

autodux

Automate the Redux boilerplate.
JavaScript
592
star
4

moneysafe

Convenient, safe money calculations in JS
JavaScript
452
star
5

credential

Easy password hashing and verification in Node. Protects against brute force, rainbow tables, and timing attacks.
JavaScript
348
star
6

rfx

Self documenting runtime interfaces.
JavaScript
277
star
7

redux-dsm

Declarative state machines for Redux: Reduce your async-state boilerplate.
JavaScript
222
star
8

speculation

JavaScript promises are made to be broken. Speculations are cancellable promises.
JavaScript
197
star
9

irecord

An immutable store that exposes an RxJS observable. Great for React.
JavaScript
192
star
10

the-software-developers-library

The Software Developer's Library: a treasure trove of books for people who love code. Curated by Eric Elliott
192
star
11

react-things

A exploratory list of React ecosystem solutions
174
star
12

express-error-handler

A graceful error handler for Express and Restify applications.
JavaScript
168
star
13

feature-toggle

A painless feature toggle system in JavaScript. Decouple development and deployment.
JavaScript
155
star
14

ogen

Write synchronous looking code & mix synchronous & asynchronous results using generators & observables.
JavaScript
129
star
15

tdd-es6-react

Examples of TDD in ES6 with React
95
star
16

react-hello

A hello world example React app
JavaScript
63
star
17

fluentjs1

Talk - Fluent JavaScript Part 1: Prototypal OO. Learn to code like a JS native. Take advantage of JavaScript's distinguishing features in order to write better code.
JavaScript
53
star
18

neurolib

Neuron emulation tools
JavaScript
52
star
19

the-two-pillars-of-javascript-talk

References from "The Two Pillars of JavaScript" talk
51
star
20

maybearray

Native JavaScript Maybes built with arrays.
JavaScript
49
star
21

qconf

Painless configuration with defaults file, environment variables, arguments, function parameters.
JavaScript
45
star
22

object-list

Treat arrays of objects like a db you can query.
JavaScript
43
star
23

gql-validate

Validate a JS object against a GraphQL schema
JavaScript
33
star
24

bunyan-request-logger

Automated request logging middleware for Express. Powered by Bunyan.
JavaScript
29
star
25

tinyapp

A minimal, modular, client-side application framework.
JavaScript
28
star
26

arraygen

Turn any array into a generator.
JavaScript
28
star
27

dpath

Dot Path - FP sugar for immutables
JavaScript
22
star
28

rootrequire

npm install --save rootrequire # then `var root = require('rootrequire'), myLib = require(root + '/path/to/lib.js');`
JavaScript
19
star
29

siren-resource

Resourceful routing with siren+json hypermedia for Express.
JavaScript
18
star
30

version-healthcheck

A plug-and-play /version route for the Node.js Express framework.
JavaScript
17
star
31

react-easy-universal

Universal Routing & Rendering with React & Redux was too hard. Now it's easy.
JavaScript
16
star
32

odotjs

A prototypal OO library for JavaScript
JavaScript
13
star
33

todotasks

TodoMVC for task runners. Not sure which task runner to use? Take a look at some reference implementations.
JavaScript
12
star
34

react-test-demo

React Test Demo
CSS
12
star
35

react-faves

Favorite React Tools
12
star
36

jiron

Make your API self documenting and your clients adaptable to API changes.
11
star
37

saas-questions

SaaS Questions: The most important questions every SaaS company should have answers to.
10
star
38

applitude

JavaScript application namespacing and module management.
JavaScript
8
star
39

grange

Generate all sorts of values based on a range of numbers.
JavaScript
7
star
40

talks-and-interviews

A list of my talks and interviews.
7
star
41

tinyerror

Easy custom errors in browsers, Node, and Applitude
JavaScript
7
star
42

jQuery.outerHTML

A tiny outerHTML shim for jQuery
JavaScript
6
star
43

connect-cache-control

Connect middleware to prevent the browser from caching the response.
JavaScript
6
star
44

nfdata

NFData - A metadata proposal for NFT interoperability
5
star
45

xforms

xforms
JavaScript
5
star
46

sparkly

Ignite Learning, Inspire Creation
CSS
5
star
47

clctr

Event emitting collections with iterators (like Backbone.Collection).
JavaScript
3
star
48

ofilter

Array.prototype.filter for objects.
JavaScript
3
star
49

rejection

You gotta lose to win.
JavaScript
3
star
50

colab

A project collaboration platform centered on live video streaming and monetization.
CSS
3
star
51

slides-intro-to-js-objects

A basic (and short) overview of objects in JavaScript.
JavaScript
2
star
52

slides-modular-javascript

Modular JavaScript With npm and Node Modules
CoffeeScript
2
star
53

testling-jasmine

run jasmine tests in all the browsers with testling
JavaScript
2
star
54

hifi

Flow based programming for Node services.
1
star
55

resource-acl

An ACL intended for use with express-resource.
1
star
56

pja-guestlist-client

The browser client for Guestlist. A demo app for Programming JavaScript Applications.
JavaScript
1
star
57

course-primer

A primer for the "Learn JavaScript with Eric Elliott" course series. Prerequisite for all courses.
1
star
58

mapall

Given an array of promises, map to a same-ordered array of resolutions or rejections.
1
star
59

pja-sections

Draft sections for "Programming JavaScript Applications"
1
star
60

maybe-assign

Like Object.assign, but skip null or undefined props.
1
star