Skip to content

Instantly share code, notes, and snippets.

@snowleo208
Last active December 14, 2018 11:19
Show Gist options
  • Save snowleo208/00edfe3fa9530be84525145c8add39d2 to your computer and use it in GitHub Desktop.
Save snowleo208/00edfe3fa9530be84525145c8add39d2 to your computer and use it in GitHub Desktop.
const inputList = Array.prototype.slice.call(document.getElementsByTagName('input')).filter(item => item.type !== 'submit' && item.type !== 'checkbox');
const input = inputList.concat(Array.prototype.slice.call(document.getElementsByTagName('textarea')));
//valid each item and set error message
function isValid(input) {
const target = input.id ? input.id : input.target.id;
const valid = document.getElementById(target).validity.valid;
if (valid) {
document.getElementById(`${target}-error`).classList.remove('invalid');
document.getElementById(`${target}-error`).removeAttribute('role');
document.getElementById(target).setAttribute('aria-invalid', 'false');
} else {
document.getElementById(`${target}-error`).classList.add('invalid');
document.getElementById(`${target}-error`).setAttribute('role', 'alert');
document.getElementById(target).setAttribute('aria-invalid', 'true');
console.log(document.getElementById(target));
}
if (document.getElementById('form-message').value.length !== length) {
length = document.getElementById('form-message').value.length;
document.getElementById('form-message-length').innerText = length;
}
return valid;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment