Skip to content

Instantly share code, notes, and snippets.

@shawnbot
Last active July 27, 2016 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shawnbot/a14c8ba59153b13f3c17ba33bf23ac07 to your computer and use it in GitHub Desktop.
Save shawnbot/a14c8ba59153b13f3c17ba33bf23ac07 to your computer and use it in GitHub Desktop.
Lightweight HTML5 form validation + ARIA
label,
input {
display: block;
}
input[aria-invalid=true][aria-label]::before {
color: red;
content: attr(aria-label);
display: block;
}
(function(exports) {
var onChange = function(e) {
var input = e.target;
var valid = input.validity.valid;
input.setAttribute('aria-invalid', !valid);
if (valid) {
// XXX: restore previous value of aria-label, e.g. via data-aria-label
input.removeAttribute('aria-label');
} else {
input.setAttribute('aria-label', input.validationMessage);
}
};
var onKeyUp = function(e) {
// only respond to keypress events for elements that have a name
if (e.target.name) {
return onChange.call(this, e);
}
};
document.registerElement('aria-validated', {
extends: 'form',
prototype: Object.create(
HTMLFormElement.prototype,
{
createdCallback: {value: function() {
// HTML5 form validation polyfill
H5F.setup(this);
}},
attachedCallback: {value: function() {
// set aria-required="true" for all required inputs
[].forEach.call(this.querySelectorAll('[required]'), function(input) {
input.setAttribute('aria-required', true);
});
this.addEventListener('change', onChange);
this.addEventListener('keyup', onKeyUp);
}},
detachedCallback: {value: function() {
this.removeEventListener('change', onChange);
this.removeEventListener('keyup', onKeyUp);
}}
}
)
});
})(this);
<link rel="stylesheet" href="aria-validated.css">
<form is="aria-validated">
<label for="first_name">First Name</label>
<input id="first_name" name="first_name" required>
</form>
<script src="aria-validated.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment