Skip to content

Instantly share code, notes, and snippets.

@fvdb
Created August 10, 2017 13:58
Show Gist options
  • Save fvdb/cc93da2e99312fa67844f5fa9bc1529d to your computer and use it in GitHub Desktop.
Save fvdb/cc93da2e99312fa67844f5fa9bc1529d to your computer and use it in GitHub Desktop.
/**
* Checks if the textarea is valid
*/
isValid() {
const { node, maximum, textarea } = this;
const { length: inputLength } = textarea.value;
if (!node.hasAttribute('data-required')) return true;
try {
guardRequired(node, inputLength);
guardMaxLength(node, inputLength, maximum);
} catch (errorMessage) {
this.errorMessage = errorMessage;
return false;
}
return true;
}
guardRequired(node, inputLength) {
if (inputLength === 0) {
throw node.getAttribute('data-message_required');
}
}
guardMaxLength(node, inputLength, maximum) {
if (maximum && inputLength > maximum) {
throw node.getAttribute('data-message_max');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment