Skip to content

Instantly share code, notes, and snippets.

@ybiquitous
Last active February 26, 2016 05:23
Show Gist options
  • Select an option

  • Save ybiquitous/a2a920d4c451915b3618 to your computer and use it in GitHub Desktop.

Select an option

Save ybiquitous/a2a920d4c451915b3618 to your computer and use it in GitHub Desktop.
input {
width: 10em;
}
input:invalid {
box-shadow: 0 0 0.5em red;
}
input ~ [data-hint] {
left: -5em;
}
<form method="POST" action="/sample">
<p><label>Name: <input type="text" required pattern="[a-z]+" minlength="3"><span class="hint--error hint--bottom hint--always" hidden></span></label></p>
<p><label>Age: <input type="number"><span class="hint--error hint--bottom hint--always" hidden></span></label></p>
<p><button>Submit</button></p>
</form>
var inputs = document.querySelectorAll('input');
for (var i = 0; i < inputs.length; i++) {
var input = inputs[i];
input.addEventListener('input', function(event) {
var el = event.target;
if (el.checkValidity()) {
el.nextElementSibling.style.display = 'none';
}
});
input.addEventListener('invalid', function(event) {
var el = event.target;
el.nextElementSibling.dataset.hint = el.validationMessage;
el.nextElementSibling.style.display = '';
});
input.checkValidity();
}
name: HTML5 Form Validation Example
resources:
- https://cdnjs.cloudflare.com/ajax/libs/hint.css/1.3.6/hint.min.css
normalize_css: no
wrap: d
@ybiquitous

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment