formJS
API
Features included in Lite version have this badge: Lite
Field Options
- beforeValidation
- focusOnRelated
- maxFileSize
Methods
- destroy
- validateField
- validateForm
Static
- as full version
Events
- fjs.field:validation
- fjs.form:destroy
- fjs.form:init
- fjs.form:validation
Validations
- as full version
Instance:
Once the instance is created, you can access to it from the DOM node of the form:
const formInstance = document.querySelector('form').formjs
and you can access these properties directly from the form instance:
-
Current group:
formInstance.currentGroup
( default value isoptions.formOptions.groups[0]
) -
Form element:
formInstance.$form
-
Options:
formInstance.options
-
Version:
formInstance.version
Options:
formJS takes an object as second parameter, options
, as follow:
{
fieldOptions: {...},
formOptions: {...}
}
Field Options
Field options that will be used for every field of the form.
You can also set specific field options for a specific field or group of checkboxes/radios via HTML attribute data-field-options
as JSON string ( see demo ).
This is the list of field options you can set:
cssClasses
focusOnRelated
maxFileSize
questionContainer
skipUIfeedback
trimValue
Each function:
Fires just before a field is being validated and it is executed as Promise ( or chain/sequence of Promises if as array ).
Additionally, the keyword this
is bound to the Form instance.
cssClasses
JS object with keys:
- dirty: ( string ) CSS class(es) to be applied if the field "dirty" ( has some value )
- error: ( string ) CSS class(es) to be applied if the field is not valid ( in any error case )
- errorEmpty: ( string ) additional CSS class(es) to be applied if the field value is empty
- errorRule: ( string ) additional CSS class(es) to be applied if the field value does not comply the validation rule
- modified: ( string ) CSS class(es) to be applied if the field has a different value than page load
- pending: ( string ) CSS class(es) to be applied when the field validation starts and removed when it finishes ( useful for async validations )
- touched: ( string ) CSS class(es) to be applied if the user blurred the field or group/form has been validated
- valid: ( string ) CSS class(es) to be applied if the field is valid
These classes are useful to show different error messages based on the error type and / or change the UI.
focusOnRelatedLite
Whether or not to focus on the related ( input / select ) field when selecting its radio answer
maxFileSizeLite
Size limit ( in MB ) for each file to upload.
Set to 0 ( zero ) if you want to disable the size check.
Can also be set on the specific field via attribute data-max-file-size
to set a custom limit.
HTML attribute data-max-file-size
is now DEPRECATED and will be removed soon.
onValidationCheckAll
If set to true
, when a field is valid, this will fire the whole form validation without feedback on the UI and will emit ONLY built-in event fjs.form:validation
.
If option groups
is set, ONLY fields of this.currentGroup
will be validated ( not the whole form ) and event emitted is fjs.group:validation
.
This is useful to check if the whole form/group is valid on each field validation.
preventPasteFields
CSS string selector for which the paste command will be disabled
questionContainer
CSS string selector used internally to find the container to add/remove CSS classes useful for feedback on UI.
skipUIfeedback
If true, the css classes for 'error' ( any type ), 'valid' and 'pending' fields and form will not be added
strictHtmlValidation
Activate JS controls for strict JS validation ( as for HTML5 attributes or emulation )
trimValue
Whether or not to trim the field value before running the validation ( select, checkbox, radio and file input are not trimmed ).
Tip: use this option with caution. If set to true and validateOnEvents
contains "input", this will prevent the user to type spaces at the beginning/end and leads to a bad user experience.
validateOnEvents
Events on which the validation will fire ( only for required fields )
Tip: always add change
to the events list in order to properly validate checkbox, radio, file inputs and selects.
For example: change input
or change blur
.
Tip: in order to validate "textual" fields ( with type text, email etc ) only on change event, you must set this option to change
. Setting this option with multiple values ( for example change input
) will fire validation for "textual" fields ONLY on input
event.
Form Options
ajaxOptions
( object )
See MDN documentation
In addition, you can set these properties:
- timeout: ( number )
- url: ( string )
NOTE: Attribute enctype="multipart/form-data"
on form tag will not set header Content-Type
since it's better to let the browser set it automatically.
Manually set header Content-Type
only for specific, manual-management cases.
Setting form attribute enctype
or header Content-Type
will allow data transformation to FormData
.
ajaxSubmit
( boolean )
To allow submit the form via AJAX.
beforeSend
( function / array of functions )
Parameters:
Each function:
-
data: ( Object )
{ formData: {}, stopExecution: false }
- formData: Object ( ONLY for AJAX forms ) with the form data
- stopExecution: Boolean ( default is false ) whether or not to prevent the form submit ( AJAX or default ).
Additionally, the keyword this
is bound to the Form instance.
Description:
Each function:
It is executed as Promise ( or chain/sequence of Promises if as array ).
For AJAX forms:
Fires before the AJAX call starts. If used to modify the form data or prevent the AJAX call to start, it must return the object data
( optionally as Promise ) passed to it.
For HTML forms:
Fire before form submit. Can be used to execute some custom code ( example: populate a hidden field ) or to prevent the form submit ( in this case, it must have a return statement as above ).
cssClasses
( object )
JS object with keys:
- ajaxComplete: ( string ) CSS class(es) to be applied when the ajax call for form submission has been completed ( useful for ajax forms )
- ajaxError: ( string ) CSS class(es) to be applied when the ajax call for form submission has finished with an error ( useful for ajax forms )
- ajaxPending: ( string ) CSS class(es) to be applied when the ajax call for form submission starts and removed when it finishes ( useful for ajax forms )
- ajaxSuccess: ( string ) CSS class(es) to be applied when the ajax call for form submission has finished with success ( useful for ajax forms )
- error: ( string ) CSS class(es) to be applied when the form is not valid
- pending: ( string ) CSS class(es) to be applied when the form validation starts and removed when it finishes
- submit: ( string ) CSS class(es) to be applied when the form submission starts and removed when it finishes ( useful for ajax forms )
- valid: ( string ) CSS class(es) to be applied when the form is valid
getFormData
( function )
Parameters:
- fields: a filtered list of form fields.
-
trimValues: boolean indicating if field values must be trimmed or not ( by default it takes the value from
fieldOptions.trimValue
- values from selects, checkboxes, radios and file inputs are not trimmed ).
These fields are excluded:
type="reset"
type="submit"
type="button"
type="file"
data-exclude-data
If you want to select fields by yourself, you can access the form element through this.$form
.
Additionally, the keyword this
is bound to the Form instance.
Description:
If set, it will be will replace the default function for "getFormData" method.
groups
( array of CSS selectors )
List of CSS selectors to define groups of validation.
If defined they will act like "steps", so, on form submit or call method validateForm
, the fields that match the selector will be valdiated ( see also method validateFieldsGroup
)
handleFileUpload
( boolean )
Whether or not to handle the file upload. It is a simple file upload via ajax.
You must also set the enctype="multipart/form-data" attribute to the form.
( NOT used if ajaxSubmit: false
)
handleSubmit
( boolean )
Description:
Whether or not to handle form submit ( AJAX or default )
If set to false, you will have to handle form submit by yourself.
nestedMultipartDataToJSON
( boolean )
Description:
Whether or not to allow, for FormData objects ( for mulitpart forms ), transformation of nested objects into JSON string.
See comment in JS section of demo Get Form Data ( Complex Structure ) for details. Same rules apply to basic structure creation.
onInitCheckFilled
( boolean )
Description:
Whether or not to validate filled fields on initialization.
See event fjs.form:init
in section Built-in Events.
Methods
destroy()Lite
Description:
This method will:
- Remove all events listeners from the form instance
- Lite Remove the form instance attached to the form element
getFormData( [trimValues] )
Description:
Get a JS object of the form data with all user answers.
Collected data depends on the field as follow:
- checkbox ( single ): boolean
- checkbox / select ( multiple ): array of strings
- radio: value set or null
- others: set / given value
Default function supports 2 ways of creating data object:
- Use field name attribute as object key ( see example in the next column )
- Use dot-notation-like way to create a nested/complex data structure ( see demo )
The function set in options.formOptions.getFormData will be used to collect data.
See Form Options Section for details.
Attribute data-exclude-data
can be used to avoid the plugin to take in consideration the field when collecting form data.
Additionally, the keyword this
is bound to the Form instance.
Parameters:
-
trimValues: ( Optional ) boolean indicating whether or not to trim field values when collecting data ( by default it takes the value from
fieldOptions.trimValue
- values from selects, checkboxes, radios and file inputs are not trimmed )
Return value:
For simple structure, an object like:
{
name: 'Valerio',
city: 'Milan',
website:'http://www.valeriodipunzio.com'
}
For complex structure, see demo
validateField( field[, fieldOptions] )Lite
Description:
Lite Fires validation for the specified field.
Then:
-
Emit event
fjs.field:validation
( which runs basic feedback on UI - with CSS classes ) -
If field is valid and
onValidationCheckAll
is set totrue
, it will not emitfjs.field:validation
for each field ( see Field Options section ) but eventfjs.form:validation
orfjs.group:validation
based on optiongroups
Parameters:
- field: CSS string selector ( or node element ) for the form field ( input, select, textarea )
- fieldOptions: ( Optional ) JS object. As described in Field Options section
Return value:
A Promise that will:
-
Resolve in
.then(() => {})
if the field is valid. -
Reject in
.catch(errors => {})
if the field is NOT valid.
In this case an object with all errors will be passed to the catch function.
Example of returned object errors
:
{
email: true,
missingAtChar: true
}
validateFieldsGroup( [group[, fieldOptions]] )
Description:
Fires validation for all fields that match the given selector.
Then:
-
Emit event
fjs.field:validation
for each field * -
Emit event
fjs.group:validation
* -
If all the fields in the group are valid, updates the value of
this.currentGroup
with the selector ingroup.next
( values ingroup
come from optiongroups
, if there's no next selector, it is set toundefined
)
* See Events section for details.
Parameters:
-
group: ( Optional ) CSS string selector to select fields to validate. Default value is
this.currentGroup
- fieldOptions: ( Optional ) JS object. As described in Field Options section
Return value:
A Promise that will:
-
Resolve in
.then(({canSubmit, fields, group}) => {})
if the group of fields is valid. -
Reject in
.catch(({fields, group}) => {})
if the group of fields is NOT valid.
Example of returned object errors
:
{
canSubmit: false,
fields: [...],
group: {
prev: Selector || undefined,
current: Selector,
next: Selector || undefined
}
}
-
canSubmit: ( Boolean )
true
only whengroup.next = undefined
that will tell you reached the last step/group. -
fields: ( Array ) same as returned by method
validateForm
but every item will haveisCheckingGroup = true
instead of isCheckingForm. -
group: ( Object )
-
prev: ( Selector || undefined ) based on current selector, the previous one from option
groups
- current: ( Selector ) selector of the group that has just been validated
-
next: ( Selector || undefined ) based on current selector, the next one from option
groups
-
prev: ( Selector || undefined ) based on current selector, the previous one from option
validateFilledFields( [fieldOptions] )
Description:
Fires validation for fields that already have a value set.
Then:
-
Emit event
fjs.field:validation
for each field *
* See Events section for details.
Parameters:
- fieldOptions: ( Optional ) JS object. As described in Field Options section
Return value:
A Promise to which will be passed an array of objects ( same array as returned by method validateForm
but will ONLY contain validated fields )
validateForm( [fieldOptions] )Lite
Description:
Lite Fires validation for the form.
If option groups
is set, the validation will act like "steps" by firing method validateFieldsGroup
( validating this.currentGroup
) each time validateForm
is called until last step, where, if fields are valid, the for is submitted as usual.
Then:
-
Emit event
fjs.field:validation
for each field * -
Emit event
fjs.form:validation
*
* See Events section for details.
Parameters:
- fieldOptions: ( Optional ) JS object. As described in Field Options section
Return value:
A Promise that will:
-
Resolve in
.then(fields => {})
if all fields are valid. -
Reject in
.catch(fields => {})
if one or more fields are NOT valid.
Example of returned array fields
:
[
{
$field: $field,
result: false,
errors: {
email: true
},
isCheckingForm: true
}
]
Static Methods
You can use these methods ONLY from the original Form class ( not from a new instance ):
Parameters:
- newErrors: JS object containing the new validation error method(s) to add ( see example below ).
The key / value pairs must be passed as:
-
key: the same used as data-subtype ( or type ) attribute, without dash and with Capital letter instead.
Eg: to validate "mobile-number" ( as data-subtype ) the rule is "mobileNumber" -
value: a function with these parameters:
- fieldValue: the string the user put in the field
- $field: the evaluated field
Notes:
This will affect both old and new instances.Can also be used to override existing validation errors.
Parameters:
- newRules: JS object containing the new validation rule(s) to add ( see example below ).
The key / value pairs must be passed as:
-
key: the same used as data-subtype ( or type ) attribute, without dash and with Capital letter instead.
Eg: to validate "mobile-number" ( as data-subtype ) the rule is "mobileNumber" -
value: a function with these parameters:
- fieldValue: the string the user put in the field
- $field: the evaluated field
- fieldOptions: fieldOptions of the form instance
{ result: true // or false }
errors
key to return the specific errors.
Notes:
This will affect both old and new instances.Can also be used to override existing validation rules.
Parameters:
- options: JS object with the fieldOptions/formOptions to override ( see Options section )
Notes:
This will affect ONLY new instancesBuilt-in Events
Each built-in event has the object event.detail
as defined in column "event detail object". By default all built-in events will bubble.
see demo
fjs.field:validationLite
Emitted just after a field has been validated.
additional parameter data
{
$field: $field,
result: false,
errors: {...},
isCheckingForm: true,
isCheckingGroup: true
}
- $field: JS node element
- result: Boolean
- errors: Object ( only in case of invalid field )
-
isCheckingForm:
true
present ONLY when event is emitted by methodvalidateForm()
-
isCheckingGroup:
true
present ONLY when event is emitted by methodvalidateFieldsGroup()
fjs.form:destroyLite
Emitted after method destroy
has been called.
fjs.form:initLite
Emitted when the form is initialized ( on constructor call ).
additional parameter data
In this case, ( if onInitCheckFilled
is set to true
in formOptions ) a Promise is returned in event.detail
and it will always go through Promise method .then
passing the array fields
to the function ( same array as returned in event fjs.form:validation
but will ONLY contain validated fields ). NOT for Lite version.
fjs.form:submit
Emitted when the form is submitted.
additional parameter data
In this case, the Promise returned by the AJAX call will be assigned to event.detail
.
fjs.form:validationLite
Emitted just after the form has been validated.
additional parameter data
{
fields: [{...}],
result: true
}
-
fields: Array of objects ( each object is like described in event
fjs.field:validation
) - result: Boolean
fjs.group:validation
Emitted just after a group of fields has been validated ( after calling method validateFieldsGroup
).
additional parameter data
{
canSubmit: false,
fields: [{...}],
group: {
prev: Selector || undefined,
current: Selector,
next: Selector || undefined
},
result: true
}
-
canSubmit: ( Boolean )
true
only whengroup.next = undefined
that will tell you reached the last step/group. -
fields: Array of objects ( each object is like described in event
fjs.field:validation
) -
group: ( Object )
-
prev: ( Selector || undefined ) based on current selector, the previous one from option
groups
- current: ( Selector ) selector of the group that has just been validated
-
next: ( Selector || undefined ) based on current selector, the next one from option
groups
-
prev: ( Selector || undefined ) based on current selector, the previous one from option
- result: Boolean
Built-in Validations
Below the rules used to validate the fields depending on html basic/data-* attributes.
You can add new validations or override existing ones - having "JS key" specified below - with addValidationRules method.
Validations can also be async, see username
validation as example.
After validation, in case of invalid field, the validation method will return the errors
object.
Errors can be returned:
-
Together with the validation
-
From a specific method in
Form.prototype.validationErrors
( the key must be the same as the validation method - validation-error methods can be added and/or overridden via static methodForm.addValidationErrors
).
In case of empty field value, errors
object will return only the "empty" error like:
{
empty: true
}
In case of invalid field value, errors
object will contain by default a key with the same name as the validation method, for example:
{
email: true,
...
}
[ val ] : Additional validation that is not part of the core library.
[ err ] : Additional error that is not part of the core library.
Additional validations can be used via import { username } from 'formjs-plugin/additional-rules'
or found in file dist/additionals/validationRules.js
( also available as ES Module in dist/additionals/validationRules.esm.js
).
Additional errors can be used via import { username } from 'formjs-plugin/additional-errors'
or found in file dist/additionals/validationErrors.js
( also available as ES Module in dist/additionals/validationErrors.esm.js
).
NOTE: some validation errors from formjs-plugin/additional-errors
may not be available.
data-subtype="alphabetic"
/^[a-z]+$/i
data-subtype="alphabetic-extended"
/^[-'A-ZÀ-ÖØ-öø-ÿ]+$/i
data-subtype="alphanumeric"
/^[\w]+$/i
data-subtype="cap"
Valid italian CAP with 5 digits
errors
object can also return these specific errors:
{
maxlength: true,
minlength: true,
invalidChars: true
}
type="checkbox"
Check if the field is checked or not.
NOTE: this validation is packed together with "checkbox" validation
data-checks="[1,3]"
Check that the number of checked checkboxes is between the the given numbers ( must be a valid - 2 values - array ).
The attribute can be applied to only one of the checkboxes, not mandatory for all.
errors
object can also return these specific errors:
{
maxChecks: true,
minChecks: true
}
type="color"
data-subtype="color"
HEX color with/without #.
Can be lowercase or uppercase.
Can be 3 or 6 characters ( 0fa | 00FFaa ).
Important Note:
type="color"
is not supported by:
- IE 11
- Safari 9.1 and earlier
type="date"
data-subtype="date"
Used together or only data-subtype ( see note below ).
Date as ISO 8601 date format with space / . - as separator:
YYYY MM DD | YYYY/MM/DD | YYYY.MM.DD | YYYY-MM-DD
Important Note:
type="date"
is not supported by:
- IE ( all versions )
- Safari for OS X ( tested on version 12 )
- Firefox 56 and earlier
If you want to support these browsers you must also set data-subtype="date"
See here for details on browsers support.
data-subtype="dateDDMMYYYY"
Cannot be used together with type="date" because it returns the date in format YYYY-MM-DD
Date as italian syntax with space / . - as separator:
DD MM YYYY | DD/MM/YYYY | DD.MM.YYYY | DD-MM-YYYY
See notes for date validation.
type="email"
data-subtype="email"
Check common email addresses instead of RFC5322 Official Standard ( see tests ).
[ err ]
errors
object can also return these specific errors:
{
missingAtChar: true,
missingUserName: true,
missingDomain: true,
missingExtensionDot: true,
missingExtension: true,
minlengthExtension: true
}
data-equal-to="password"
The "name" attribute of the field this answer value must be equal to
Eg: to confirm a field for email/password
data-exact-length="24"
Check that the input length is equal to the data-exact-length value.
Eg: a field that must have a specific number of characters, such as for cents ( 2 digits )
errors
object can also return these specific errors:
{
maxlength: true,
minlength: true
}
type="file"
DEPRECATED
File size limit ( in MB ) can be specified like:
data-max-file-size="10"
Use fieldOptions.maxFileSize
or attribute data-field-options
instead of data-max-file-size
Check if a file is selected or not.
accept
attribute is supported for JS validation.
errors
object can also return these specific errors:
{
maxFileSize: true,
acceptedFileType: true
}
data-subtype="fiscal-code"
Check italian fiscal code ( read this post ).
data-subtype="landline-number"
Landline prefix:
+39 | 0039 | not-set ( also with international prefixes with 2 digits )
Landline numbers must start with 0 ( as for italian ones ):
1234567890 | 12 34567890 | 123456789 | 1234 56789 ( also with . - / as separator )
data-length="[5,30]"
Check that the field value length is between the the given numbers ( must be a valid - 2 values - array ).
With Numbers
max="13"
With Dates
max="2019-02-22"
with custom date validation ( set via data-subtype
) you must also set data-date-format
with one of the following values ( date separator can be one of - / . " " ):
-
YYYY-MM-DD
( default ) -
DD-MM-YYYY
-
MM-DD-YYYY
Example:
data-date-format="DD-MM-YYYY"
HTML5
Check that the input number is less or equal to the max value.
maxlength="150"
HTML5
Check that the input length is less or equal to the maxlength value.
With Numbers
max="7"
With Dates
max="1900-01-01"
with custom date validation ( set via data-subtype
) you must also set data-date-format
with one of the following values ( date separator can be one of - / . " " ):
-
YYYY-MM-DD
( default ) -
DD-MM-YYYY
-
MM-DD-YYYY
Example:
data-date-format="DD-MM-YYYY"
HTML5
Check that the input number is more or equal to the min value.
minlength="10"
HTML5
Check that the input length is more or equal to the minlength value.
data-subtype="mobile-number"
Mobile number must start with "3"
+39 | 0039 | not-set ( also with international prefixes with 2 digits )
3234567890 | 323 4567890 ( also with . or - as separator )
type="number"
data-subtype="number"
Also used together.
Check if it is a number ( both integer and floating are valid )
type="number"
data-subtype="number-float"
Used together or only data-subtype.
Check if it is a floating number.
Valid numbers: 123.456 | .123
type="number"
data-subtype="number-integer"
Used together or only data-subtype.
Check if it is an integer number.
type="password"
data-subtype="password"
Password ( no special characters allowed ) with at least:
- one digit
- one lowercase
- one uppercase
- min length of 8 characters
errors
object can also return these specific errors:
{
minlength: true,
missingNumber: true,
missingLowercase: true,
missingUppercase: true,
invalidChars: true
}
pattern="[RegExp]"
HTML5
Check the field value against the RegExp in pattern attribute.
type="radio"
Check if the field is checked or not.
data-require-more="" id="element-that-requires-more"
data-required-from="#element-that-requires-more"
Attributes data-require-more
and id
must be set on the input field ( ideally a radio input ) that, if selected, requires an extra answer to be valid.
Attribute data-required-from
must be set to a field ( ideally a text input ) and the attribute value must be a CSS selector ( the "id" in the API example ) of the related input ( the one specified before ).
The field with attribute data-required-from
can have other validation attributes to set specific validations.
type="tel"
data-subtype="tel"
Validate a phone number.
Check if "mobileNumber" or "landlineNumber" is valid.
Important Note:
type="tel"
is supported by:
- Safari 8 and newer
data-subtype="url"
Must not contain parameters:
www.mysite.com/index.html --> VALID URL
www.mysite.com/index.html?v=hello --> INVALID URL
data-subtype="username"
Input field must also have this attribute to specify the url of the async validation:
data-async-validation-url="path-to/async-validation"
Username must respect these criteria:
- Start with: a letter or a number or an underscore
- Can contain . - @
- Min length of 3 characters
- Max length of 24 characters
errors
object can also return these specific errors:
{
minlength: true,
maxlength: true,
invalidChars: true,
invalidStartChar: true,
ajaxCall: true
}
data-validate-if-filled
The field will be validated only if filled.
If empty, the field will be evaluated as valid.
data-subtype="vat-number"
VAT number can contain or not the string "it" ( as for italian VAT numbers ) and then 11 numbers
errors
object can also return these specific errors:
{
minlength: true,
maxlength: true
}
Utils
There are some extra features you can use from formjs-plugin/utils
, for example:
import { initCharLengthFields } from 'formjs-plugin/utils'
initCharLengthFields()
Description:
Initialize character count for fields.
HTML example:
<div data-formjs-question class="form-group">
<div class="answers-box label-move-up">
<textarea name="char-count-message" class="form-control" rows="3" maxlength="100" data-char-count></textarea>
<label>Message ( <span data-char-length></span>/<span data-char-maxlength></span> )</label>
</div>
</div>
-
data-char-count
the field you want to count the characters on -
data-char-length
where you want to show the chatacters counter -
data-char-maxlength
where you want to show the maximum length of the field
initMaxFileSizeFields()
Description:
Initialize file fields to show the max file size.
HTML example:
<div data-formjs-question class="form-group">
<div class="answers-box label-move-up">
<input name="file-required" type="file" class="form-control" data-max-file-size="2" required />
<label>Upload file</label>
</div>
<div class="field-error-message small">
This is mandatory and not valid! ( max file size is: <span data-print-max-file-size></span>MB )
</div>
</div>
-
data-print-max-file-size
where you want to show the maximum size of the file to upload