Trigger plugin
Indicate the events which the validation will be executed when these events are triggered
Usage
By default, the field will be validated automatically when the input
event is triggered. This plugin allows to set the specific events for given field as fllowing:
<html>
<head>
<link rel="stylesheet" href="/vendors/@form-validation/umd/styles/index.min.css" />
</head>
<body>
<form id="demoForm" method="POST">...</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.3/es6-shim.min.js"></script>
<script src="/vendors/@form-validation/umd/bundle/popular.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function(e) {
FormValidation.formValidation(
document.getElementById('demoForm'),
{
fields: {
...
},
plugins: {
trigger: new FormValidation.plugins.Trigger({
event: ...,
threshold: ...,
delay: ...,
}),
...
},
}
);
});
</script>
</body>
</html>
The sample code above assumes that the FormValidation files are placed inside the vendors
directory. You might need to change the path depending on where you place them on the server.
Options
Option | Type | Description |
---|
event | String or Object | Define the events |
The event
option can be one of the following formats:
event: 'blur',
event: 'blur change',
event: {
fullName: 'blur',
email: 'change',
},
event: {
email: false,
},
Option | Type | Description |
---|
threshold | Number or Object | Only perform the validation if the field value exceed this number of characters. By default, it's set to 0. This option does not effect to the elements which user cannot type, such as radio, checkbox, select one. |
threshold: 5,
threshold: {
fullName: 15,
email: 10,
},
Option | Type | Description |
---|
delay | Number or Object | Pending validation for a given number of seconds after user stops filling in the field. By default, it's set to 0. It's really useful if the field needs to perform validation in server side, such as the remote validator |
delay: 5,
delay: {
fullName: 0,
username: 5,
},
Using the npm packages
If you are using a bundler such as
Webpack,
Rollup,
Parcel or
Vite, etc., to bundle
your application, then it's recommended to use the FormValidation NPM packages.
$ npm install @form-validation/bundle
$ npm install @form-validation/plugin-trigger
- Import and use the Trigger plugin:
import { formValidation } from '@form-validation/bundle/popular';
import { Trigger } from '@form-validation/plugin-trigger';
formValidation(
document.getElementById('demoForm'),
{
fields: {
...
},
plugins: {
trigger: new Trigger({
event: ...,
threshold: ...,
delay: ...,
}),
...
},
}
);
Basic example
In the following form, the Title field will be validated while user type any character (trigger="keyup"
). The Summary field will be validated when user lose the focus (trigger="blur"
).
See also
Changelog
- The plugin throws an exception when there is a field whose name is a property of
String.prototype
such as link
- Added a new filter named
plugins-trigger-should-validate
. We can use it to determine the field is validated automatically when the value is changed or not.