• Stars
    star
    740
  • Rank 61,282 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 14 years ago
  • Updated about 8 years ago

Reviews

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

Repository Details

Deprecated, please use hyperform instead https://github.com/hyperform/hyperform

H5F Build Status

Deprecated

This hasn't been maintained in quite a while and recently a much better library has been released, hyperforms! I highly recommend you use that.

===

a JavaScript library that allows you to use the HTML5 Forms chapters new field input types, attributes and constraint validation API in non-supporting browsers.

The H5F script will detect if the browser has support for the HTML5 Forms Chapter and either hook into the native methods, attributes and events or emulate the new features in non-supporting browsers.

What's supported

H5F offers support for most, but not all, of the HTML5 Forms Chapter:

  • Input types: email and url
  • Input attributes: pattern, placeholder, min, max, step, required
  • Textarea attributes: placeholder and required
  • Select attributes: required
  • Form attributes: novalidate
  • Submit attributes: formnovalidate

Also supported is the constraint validation API:

  • Field validity object
  • checkValidity() method on form or individual field
  • setCustomValidity() method to set custom error message
  • validationMessage attribute that returns the message set using setCustomValidity() method

Example

<form id="signup">
	<label>Email</label>
	<input type="email" placeholder="e.g. [email protected]" required />

	<label>Phone</label>
	<input type="tel" id="tel" name="tel" pattern="\d{10}" />

	<label>Post code *</label>
	<input type="number" min="1001" max="8000" required />

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

On page load you run the H5F setup method:

H5F.setup(document.getElementById("signup"));

For a working demo download the demo files.

Setting custom error message

With setCustomValidity() method you can set a custom error message on a field and it will be in an invalid state until the custom message is set back to an empty string.

document.getElementById("other").setCustomValidity("Please enter some information");

This field will be in a permanent invalid state, we can return the custom error message by using the validationMessage attribute

document.getElementById("other").validationMessage;
// "Please enter some information"

A good use case for this functionality is a password comparison field.

var pass = document.getElementById("pass"),
	cpass = document.getElementById("cpass");

if(cpass.value !== pass.value) {
	cpass.setCustomValidity("Your password doesn't match");
} else {
	cpass.setCustomValidity("");
}

Passing multiple forms

You can pass an HTMLFormElement, HTMLCollection of HTMLFormElements, or Array of HTMLFormElements.

H5F.setup([document.getElementById("form1"),document.getElementById("form2"),document.getElementById("form3")]);

Optional settings argument

The H5F.setup method also accepts a second optional argument so you can specify the fields validation class names:

H5F.setup(document.getElementById("signup"), {
	validClass: "valid",
	invalidClass: "invalid",
	requiredClass: "required",
	placeholderClass: "placeholder"
});

As well as form submit and invalid field event handlers, in case you need to hook custom functionality:

H5F.setup(document.getElementById("signup"), {
	onSubmit: function (event) {},
	onInvalid: function (invalidInputElement) {}
});

If you need to submit your form through AJAX, you must add your event handler using the onSubmit option, else, your handler will get called even if H5F invalidates the form. See A word on AJAX form submission under Form submission blocking.

onInvalid lets you add a callback for invalid fields to help you showing or doing special things when fields invalidate.

Form submission blocking

HTML5 forms will block form submission until the form is valid this can be switched off by setting the novalidate attribute on the parent form element.

<form id="signup" novalidate>
	<label>Email</label>
	<input type="email" placeholder="e.g. [email protected]" required />

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

The above form regardless of attributes set won't validate and will submit, default behaviour is to block form submission.

You can also specify the formnovalidate attribute on a submittable element e.g. input or button. The attribute will bypass any form validation but only if the button with the formnovalidate attribute is clicked.

<form id="signup">
	<label>Email</label>
	<input type="email" placeholder="e.g. [email protected]" required />

	<input type="submit" value="Save" formnovalidate />
</form>
A word on AJAX form submission

If you're going to submit your form through AJAX, you may be attaching a event handler to the form submit event. This handler will be called even if form is invalid for H5F. While we could avoid this using Event.prototype.stopImmediatePropagation() the reality is that older browsers don't implement it. So the workaround for this is that you add your event handlers using the provided onSubmit setup option.

http://thecssninja.com/javascript/H5F

License

Copyright (c) 2012 Ryan Seddon Licensed under the MIT license.

More Repositories

1

react-frame-component

Render your React app to an iFrame
JavaScript
1,748
star
2

bunyip

Automate client-side unit testing in real browsers using the CLI
JavaScript
382
star
3

source-map

This is an unconnected fork of Mozilla's source-map library
JavaScript
227
star
4

60fps-scroll

Deprecated - A little library that *could* make your site scroll faster
JavaScript
213
star
5

redux-debounced

Debounce middleware for Redux
JavaScript
166
star
6

slideshowbob

The evil, fumbling, JavaScript free, slide thingy
CSS
98
star
7

ScrollListView

Performant list view that re-uses cells to display large data sets
JavaScript
80
star
8

font-dragr

Drag and drop font testing
HTML
68
star
9

gulp-es6-module-transpiler

JavaScript
61
star
10

bootleg

Bootleg: Dodgy JavaScript free bootstrap plugins
HTML
60
star
11

earthin24

Source code for my twitter bot https://twitter.com/earthin24
Go
22
star
12

yeti-adaptors

Use Yeti with other client-side testing frameworks
JavaScript
18
star
13

sourcemap-onerror

Lookup original location from window.onerror by querying source map
JavaScript
18
star
14

web-components

A talk about web components. What they are and what they offer.
CSS
17
star
15

BrowserSocket

Create a socket server in the browser
JavaScript
15
star
16

sunrise-lifx

Simulate a sunrise effect using the Lifx ruby gem
Ruby
15
star
17

markdown-component

A Web Component to parse markdown to html
14
star
18

picture-component

<x-picture> element for responsive images
6
star
19

SUX

SUX.js a little JavaScript helper to expand short URLs
JavaScript
6
star
20

spaces-talk

Super serious talk on why spaces are the best and you're an idiot if you use tabs
JavaScript
6
star
21

FSA

JavaScript library to give you some helper methods to read and write files or directories using the new File API: Directories and System API
JavaScript
5
star
22

slideshow-component

Every developer who does a talk on Web Components inevitably creates a slideshow component
4
star
23

jemena-powershop

Jemena + powershop = almost realtime total power usage
JavaScript
4
star
24

solar-dashboard

A solar dashboard for the Adafruit MagTag
Python
4
star
25

flexbox-wds13

Flexbox talk for Web Diretions South 2013
JavaScript
3
star
26

travis-ci-templates

A series of templates to get you started with travis-ci integration in your repo
JavaScript
3
star
27

webcomponents-talk

The Composable Web with Web Components
CSS
2
star
28

es6-module-macro

ES6 Module macro that expands to commonjs
JavaScript
1
star
29

css-switch

A simple recreation of the iOS toggle switch using CSS
CSS
1
star
30

layouts

Demo jekyll templates for my projects
1
star
31

webpack-talk

A short look at webpack a module bundler for building web applications
CSS
1
star
32

dumplingsjs.com

Dumplings and JavaScript
1
star
33

bespoke-dir

Add a class to the slide parent element to let you know which direction the slides are going
JavaScript
1
star
34

source-map-workflows

A collection of workflows for client side apps that involve creating source maps
1
star