vally
vally is a simple ES6, zero-dependency form field validation library, that helps
to determine if <input>
-values pass certain tests.
The library just provides a set of useful helper functions. Most of the DOM-manipulation
and validation handling still lies in the hands of the user to ensure as much
flexibility as possible.
ANNOUNCEMENT:
At this point I would currently advise against the use of vally. I am working heavily on a rewrite, which should be out in a couple of days. This v2.0.0 will contain a lot of breaking changes, but will make this library way more flexible. If the current state of vally is exactly what you are looking for, feel free to continue using it (or just fork it). However I will not continue to develop v1.x.x.
Installation
npm i --save vally
You can either use the build from inside the dist/
directory and include
it via <script>
-tag or use require
/ import
-syntax in your code.
or
// => false
API
Docs
Configuration and API details can be found here:
FAQ
How can i specify a field as required?
Just use the regular required-Attribute on your
<input>
. This will ensure that vally actually validates the element if the input is empty. You still have to specify validator functions to provide the actual validation functionality. I.e. if the field should not be empty useisNoneEmptyString()
.
What happens to inputs that are not displayed?
Inputs that are not displayed (i.e. if
display: none
,type="hidden"
orhidden=""
is set), are simply ignored by the validation.
How can I bind vally to certain events?
vally leaves most of the DOM-manipulation to the user. For simple bindings (i.e. for 'keyup'-events) however you can use
initWithBindings()
. For detailed explaination have a look at our examples below.
Why does vally ship so few validator functions?
Because validators in vally are simple functions it is very easy to either write them yourself our just use a library like validator.js. Providing only a basic set of functions keeps the footprint of the library small.
How can I use a validator function with multiple arguments?
If you need multiple arguments (like some validator.js. functions need additional configuration) you can simply partially apply the function and return a validator function.
Example:
const isLessThan = val: any: { /* actual implementation */ }
Examples
Simple Example
Simple submit Some number: Mail*: Custom (start with 'T')*: Submit
Complex example
The following example shows how vally can be used to use the same configuration
to manually validate on the submit
event and also bind it to
fire on keyup
triggered by individual inputs.
index.html
Lets use almost the same markup as before... . This time we ship vally bundled together
with our other js resources in main.bundle.js
, though. We also want to insert
custom error messages into the DOM depending on which validator for a field failed.
There are a lot of ways to achieve this. In this case we simply put hidden divs
below each input and toggle their display
on validation.
Of course we also insert our custom messages into them.
Simple submit Some number: Mail(*): Number below 10(*): Submit
config.js
We separate our configuraion from the actual validation logic, to make everything a bit more maintainable.
// config.js // Custom validator// Because we need another parameter for our function to specify the threshold,// we simply curry our validator function. The actual invokation to get a// real validator function would look like this: isLessThan(10)const isLessThan = val: any: { if return false return < num} // Because we only want to fetch DOM-elements via document.querySelector// we can use the createConfig helper function to create a valid configuration.// Therefore we specify our specs with selectors, which in turn are used to// fetch the actual DOM nodesconst specs = selector: '#mail' validators: fn: isNoneEmptyString errorSelector: 'mail-error' msg: 'Please enter a value.' fn: isEmail errorSelector: 'mail-error' msg: 'Please enter a valid email address.' selector: '#number' validators: fn: isNumberString errorSelector: 'number-error' msg: 'Please enter a number.' selector: '#custom' validators: fn: isNoneEmptyString errorSelector: 'custom-error' msg: 'Please enter a value.' fn: errorSelector: 'custom-error' msg: 'Please enter a number smaller than ten.'
index.js
Here we will define our actual validation logic.
// index.js // Our callback will recieve a result object and act on its field validationsconst callback = e: Event validations : validations: Valiations : { validations} // Create a pre-configured partially applied validate function that we can use// on our submit button. We technically don't need to do this// as we only need to validate on submit. But in a real-world application you might// need to re-use your validate function// and this makes it easier.const validateForm = const init = { // Bind our callback to the keyup event on each individual field // Bind our validate function to our submit button const btnSubmit = document if !btnSubmit return btnSubmit}