jQuery Validity
A form validation jQuery plugin written in late 2015 (whatever that means).
This is heavily inspired by the Abide library (from the Zurb Foundation framework), but implemented completely from scratch and as a jQuery plugin, npm-ready and most of all not bloated.
Install
Using npm
:
npm install jquery-validity
Browserify and others:
var $ = require('jquery')
require('jquery-validity')($)
ES2015 modules:
import $ from 'jquery'
import validity from 'jquery-validity'
validity($)
Usage
$('.my-form').validity(options)
Options
There are the defaults:
$.fn.validity.defaults = {
attributeName: 'data-validators',
errorClass: 'error',
focusOnFirstError: true,
onSubmit: null,
parentSelector: 'p',
requiredMessage: 'This field is required',
timeout: 1000,
validateOnBlur: true,
validators: {}
}
Events
$form.on('validity.invalid', {
fields,
firstErrorField,
form,
submitEvent
})
$form.on('validity.valid', {
form,
submitEvent
})
(TODO)
An example of an email validator function:
function emailValidator (message) {
return function (value) {
if (validator.isEmail(value)) {
return null
}
return message
}
}
TODO
- Proper tests
- Finish documentation
- A better usage guide and examples
License
MIT