Skip to content

Instantly share code, notes, and snippets.

@jeroenransijn
Created January 13, 2017 18:54
Show Gist options
  • Save jeroenransijn/283167a527551e4c62905e72083cb944 to your computer and use it in GitHub Desktop.
Save jeroenransijn/283167a527551e4c62905e72083cb944 to your computer and use it in GitHub Desktop.
const getValidationRules = (el) => {
const rules = [];
const required = el.hasAttribute('required')
const min = el.getAttribute('min')
const max = el.getAttribute('max')
const maxlength = el.getAttribute('maxlength')
const minlength = el.getAttribute('minlength')
const pattern = el.getAttribute('pattern')
if (required) {
rules.push(val => val.length > 0)
}
if (min !== null) {
rules.push(val => val >= min)
}
if (max !== null) {
rules.push(val => val <= max)
}
if (minlength !== null) {
rules.push(val => val.length >= minlength)
}
if (maxlength !== null) {
rules.push(val => val.length <= maxlength)
}
if (pattern !== null) {
rules.push(val => val.match(new RegExp(pattern)))
}
return rules
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment