Created
January 13, 2017 18:54
-
-
Save jeroenransijn/283167a527551e4c62905e72083cb944 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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