formJS

JavaScript Form Validation.
Made Easy

Use standard HTML to enable form superpowers.
Need only basic powers? Try Lite version.

Version

Only 7.6KB ( Lite version 3.5KB ) minified & gzipped

Download Latest Version | All Releases

Need a break?

Yep! I'd like to book
a room for

how many guests?


in

where do you wanna go?

,
sleep there for

min 2, max 20

nights
starting from

when will you arrive?

Browsers Support

formJS is compatible with ( desktop & mobile versions ):


* for compatibility with IE, old versions of Edge and/or other old browsers you may need to load some JS polyfills ( from polyfill.io or else ):
Array.from Element.prototype.closest Element.prototype.matches Promise Promise.prototype.finally AbortController fetch CustomEvent

<script src="https://cdn.polyfill.io/v3/polyfill.min.js?features=Array.from,Element.prototype.closest,Element.prototype.matches,Promise,Promise.prototype.finally,AbortController,fetch,CustomEvent"></script>

* for Lite version you may need these polyfills:
Array.from Element.prototype.closest Element.prototype.matches Promise CustomEvent

Changelog

  • 5.3.2
    • Fix bug on "destroy" method: custom event listener for group validation was not removed ( full version only )
  • 5.3.1
    • Removed "dist/additionals" from .npmignore to allow import additionals
  • 5.3.0
    • Added "exports" to package.json in order to export also lite version and extra features
    • Add "utils" to exported items
    • Added option "groups" in "formOptions" to enable validation steps/groups ( full version only )
    • Added instance prop "currentGroup" ( full version only )
    • Added method "validateFieldsGroup" ( full version only )
    • Added event "fjs.group:validation" ( full version only )
    • Fixed bug on method "validateForm" to fire event "fjs.field:validation" for each filed after validation ( Lite version only )
    • Minor internal improvements
    • DEPRECATED features ( will be dropped in v6 ):
      • Support for IE and old browsers
      • SystemJS version
  • 5.2.0
    • Added method "validateFilledFields" ( full version only )
    • Update "getFormData" to support object notation in fields name in returned js object ( full version only )
    • Added support for custom events for Lite version
    • Minor improvements
  • 5.1.0
    • Added "trimValue" in "fieldOptions" ( full version only )
    • Added argument "trimValues" for method "getFormData" ( full version only )
    • Added HTML attribute "data-field-options" to enable specific field options for single fields
    • Added "fieldOptions" to data passed to "beforeValidation" callback(s)
    • Re-introduced "maxFileSize" in "fieldOptions"
    • Re-introduced "fieldOptions" argument passed to validation rules
    • Fixed bug where css class(es) for "pending" were not removed when enabling "onValidationCheckAll" ( full version only )
    • DEPRECATED features:
      • HTML attribute "data-max-file-size" in favor of "maxFileSize" in "fieldOptions" and/or HTML attribute "data-field-options"
  • 5.0.2
    • Enabled validation for "change" event only ( full version only )
  • 5.0.1
    • Exported main versions as UMD instead of IIFE for better environments compatibility
  • 5.0.0
    • Added event "fjs.form:init"
    • Added "onInitCheckFilled" in formOptions to run or not validation of filled fields on form init ( same as removed method "init" )
    • Changed Promise returned from methods "validateField" and "validateForm". Now if validation succeeds it will resolve with .then(), if validation fails it will reject with .catch()
    • Changed "fieldEl" with "$field" and "formEl" with "$form"
    • Changed key "stringLength" in favor of "length" in built-in errors
    • Removed method "init" in favor of option "onInitCheckFilled"
    • Removed "handleValidation" and "maxFileSize" in fieldOptions
    • Removed fieldOptions parameter in validation-rule methods
    • Removed "rule" in built-in errors
    • Moved option "handleFileUpload" from fieldOptions to formOptions for consistency
    • Moved "email" validation-error method from core to additionals ( external )
    • Switched usage of JS Event with CustomEvent
    • Minor improvements
  • 4.4.0
    • Added "trimValue" in "fieldOptions" ( full version only )
    • Added argument "trimValues" for method "getFotmData" ( full version only )
    • Added HTML attribute "data-field-options" to enable specific field options for single fields
    • Added "fieldOptions" to data passed to "beforeValidation" callback(s)
    • Fixed bug where "fieldOptions" argument was not declared in validation method for input type "file"
    • Fixed bug where css class(es) for "pending" were not removed when enabling fieldOptions.onValidationCheckAll ( full version only )
    • DEPRECATED features:
      • HTML attribute "data-max-file-size" in favor of "maxFileSize" in "fieldOptions" and/or HTML attribute "data-field-options"
  • 4.3.4
    • Enabled validation for "change" event only ( full version only )
      NOTE: This patch was released after publishing v5.0.0, so it's shipped in v5 only from v5.0.2
  • 4.3.3
    • Exported main versions as UMD instead of IIFE for better environments compatibility
      NOTE: This patch was released after publishing v5.0.0, so it's shipped in v5 only from v5.0.1
  • 4.3.2
    • Fixed bug on fetch response not ok ( full version only )
    • Updated custom validation for username ( in external validations )
  • 4.3.1
    • Fixed bug for validation on "focus" event ( full version only )
  • 4.3.0
    • Added "event.data.isCheckingForm = true" for event "fjs.field:validation" emitted on validateForm() ( full version only )
  • 4.2.3
    • Added ESM version for additional validations and errors in dist folder
    • Minor improvements for full version only:
      • Improved code in validateField when "onValidationCheckAll" is set to true
      • Calling method validateForm() will emit event "fjs.field:validation" for each validated field
      • When calling method init(), to avoid focus on related fields, focusOnRelated is set to false and then restored
      • Removed additional event emitted on form while validating a field since event emitted on field will bubble
  • 4.2.2
    • Minor improvement after field validation when onValidationCheckAll is true
  • 4.2.1
    • Fixed bug on options merge when extending Form class
    • Updated dev dependencies
  • 4.2.0
    • Custom error key(s) are now dynamically added for each valdiation-rule based on validation-rule name
    • Fixed bug in method "init" where field "data-equal-to" was returned twice instead of its comparison field
    • Minor improvements
    • DEPRECATED features:
      • Method name "init" will be changed in the next major release
      • Key "stringLength" in favor of "length" in built-in errors
  • 4.1.0
    • Added "questionContainer" in fieldOptions
    • Added "data-date-format" in combination with "min" and "max" attributes for custom date validation
    • Added Lite version of formJS
    • Added Jest tests
    • When valdiating a filed with method validateField and it's valid, the automatic form validation ( if "onValidationCheckAll" is set to true ) will skip this field
    • Now all validation rules and errors can be overridden
    • To improve consistency, now validations don't throw custom errors ( via new Error(...) in try/catch blocks ) anymore but default ones.
    • Internal improvements
    • DEPRECATED features:
      • fieldOptions.handleValidation
      • fieldOptions.maxFileSize
      • Email validation-error method
      • Key "rule" in built-in errors
      • "fieldOptions" parameter for validation rules
  • 4.0.1
    • Replaced Webpack with Rollup
    • FormJS is now available as ES Module and SystemJS in "dist" folder and via CDNs
    • Reduced plugin size
    • Changed RegExp for email validation to check common email addresses instead of RFC5322 Official Standard
  • 4.0.0
    • Method "init" now returns a Promise
    • Added custom event listener for form submit
    • Added custom events to replace callback options ( but "onPastePrevented" )
      • Removed callbacks "onPastePrevented" and "onValidation" from "fieldOptions"
      • Removed callbacks "onSubmitComplete", "onSubmitError" and "onSubmitSuccess" from "formOptions"
    • Code refactoring to improve performances and reduce plugin size
  • 3.3.2
    • Fixed bug in "date" validation rule
  • 3.3.1
    • Fixed bug in method "validateField" when the field is not valid
  • 3.3.0
    • Added "cssClasses" in "formOptions" with "ajaxComplete", "ajaxError", "ajaxPending", "ajaxSuccess", "submit", "valid"
  • 3.2.1
    • Added "beforeValidation" in "fieldOptions"
    • Added "pending" to "cssClasses" in "fieldOptions"
    • Added "onValidationCheckAll" in "fieldOptions"
    • Now "beforeSend" in "formOptions" can also return an object instead of only a Promise
    • Now "onValidation" in "fieldOptions" is triggered every time a field is evaluated as valid ( to check the entire form validity )
    • Attribute "novalidate" is now forced to true
    • Updated dev dependencies
    • Minor updates and fixes
  • 3.1.2
    • Updated file .npmignore
  • 3.1.1
    • Project published on NPM.
    • Updated webpack config to export library as UMD Module.
    • Removed "FormJS" as alternative namespace.
    • Fixed bug in constructor function that caused an error when using the library as ES module.
  • 3.1.0
    • beforeSend callbacks are now promise-based
    • Added 2-ways validation for fields with attribute data-equal-to
    • Added validations for "alphabetic", "alphabeticExtended" and "Alphanumeric" to the external file /dist/additionals/validationRules.js
    • Simplified data returned in ajax response, now returning only the data coming from the call.
    • Moved validations for "numberFloat" and "numberInteger" to the external file /dist/additionals/validationRules.js
    • Minor bugfix and improvements
  • 3.0.0
    NEW MAJOR VERSION

    formJS is now available on jsDelivr CDN.

    formJS is now available in 2 versions ( the difference is only about the API used for AJAX calls ):

    • fetch
    • XMLHttpRequest

    Many relevant changes from the previous version:

    • Added async validation with Promises.
    • Added built-in errors: prototype method "addValidationErrors" and object "validationErrors".
    • Added "errorEmpty" and "errorRule" to "cssClasses" in "fieldOptions" to show different error messages ( see implementation in CSS file ).
    • Added validation rules for:
      • "color" ( input type="color" - as replace of "hexColor" )
      • "dateDDMMYYYY"
      • "tel" ( input type="tel" )
    • Added support for attributes "min" and "max" for date fields.
    • Added "tempOptions" parameter to "onPastePrevented", "onValidation", "beforeSend", "onSubmitComplete", "onSubmitError", "onSubmitSuccess" callback functions.
    • Renamed method "getFormJSON" with "getFormData" ( and related option in "formOptions" ).
    • Renamed method "isValidField" with "validateField".
    • Renamed method "isValidForm" with "validateForm".
    • Renamed attribute "data-exclude-json" with "data-exclude-data" for consistency with related "getFormData" method.
    • Changed "ajaxData" parameter in "onSubmitComplete", "onSubmitError", "onSubmitSuccess" to be compliant with JSend standard.
    • Removed "Utilities" since it was going outside the plugin's scope.
    • Removed "submit" method since it was not so useful.
    • Removed the parameter from "getFormData" method. Now the function to collect data is taken in options.formOptions.getFotmData.
    • Removed "checkDirtyField" option from "fieldOptions" ( the related callback is automatically executed ).
    • Removed some polyfills thanks to code changes.
    • Minor improvements.
  • 2.3.3
    • Fixed a bug on fields "data-require-more"/"data-required-from".
    • Little improvement on submit method.
  • 2.3.2
    • Fixed a bug that caused field "data-required-from" to be validated even if not mandatory.
    • Fixed a bug that caused options functions ( in case on array of functions, like onValidation ) to be incorrectly merged.
  • 2.3.1
    • Fixed a bug that caused checkboxes and radios to be validated twice ( on Chrome ) if "validateOnEvents" option in "fieldOptions" was set to "change blur".
  • 2.3.0
    • Added "init" method.
    • Added validation for data-type="number" to emulate type="number" inputs ( see "strictHtmlValidation" option in Field Options section for details ).
    • Modified the JSON data returned from "getFormJSON" method in order to get a boolean in case of single checkbox field and an array of the strings in case of multi-checkbox ( in this case every checkbox MUST have a value set ). Same logic for select and multi-select fields.
    • Fixed a bug that caused a field with "maxlength" and "required" attributes to be evaluated as valid if the field value was empty.
    • If "checkDirtyField" option is set to true, the "dirty CSS class" will now be applied to the container element with "data-formjs-question" attribute - or the field itself ( as in previous versions ), if not.
    • Changed default option value for "fieldOptions.cssClasses.dirty" from "dirty-field" to "is-dirty".
    • Now the JS field element is passed to each validation rule as second argument.
    • Updated polyfill url to point to thw new v3.
    • Minor code improvements.
  • 2.2.1
    • Fixed a bug in "beforeSend" callback that prevented the script to be stopped even with "stopExecution" set ot true.
  • 2.2.0
    • Added "handleValidation" in "fieldOptions"
    • Added "ajaxOptions" in "formOptions"
    • Added "getFormJSON" in "formOptions"
    • Added "customFn" parameter to "getFormJSON" method
    • Moved "handleFileUpload" from "formOptions" to "fieldOptions" for options consistency
    • Removed support for attributes "data-ajax-settings" and "data-formjs-init". Use instead, respectively, "ajaxOptions" in "formOptions" and "handleValidation" in "fieldOptions" & "handleSubmit" in "formOptions"
    • "onPastePrevented" and "onValidation" in "fieldOptions" but also "beforeSend", "onSubmitComplete", "onSubmitError" and "onSubmitSuccess" in "formOptions" can now be a function or array of functions
  • 2.1.0
    • Added "destroy" method.
    • Added "handleSubmit" option in "formOptions".
    • Added "Checks" and "Length" in "Built-in Validations".
    • Removed "errorMultiChoice" in "fieldOptions.cssClasses".
    • Removed support for "data-ajax-submit" attribute and added "ajaxSubmit" option in "formOptions" instead.
    • Removed "Max check" from "Built-in Validations". Use the new "Checks" validation.
    • Now "formEl" in constructor function can also be a CSS string selector.
    • Better check for "formEl" argument inside the constructor function.
    • Renamed "manageFileUpload" option in "handleFileUpload" inside "formOptions".
    • Changed RegExp for "Date" in "Built-in Validations".
    • Some internal improvements.
  • 2.0.0
    Rewritten with ES6:
    • Completely different usage of the plugin with introduction of plugin instance instead of one-instance-management.
    • Changed "submitAjaxForm" method with "submit" method ( manages both AJAX and HTML forms ).
    • Moved "addValidationRules" from instance methods to Class-prototype methods.
    • Changed regex for email validation.
    • Added "setOptions" method ( as Class-prototype method ).
    • Some changes in methods arguments and callbacks arguments.
    • Now HTML forms are better managed and beforeSend function is available.
    • Added support for JS validation of "minlength" attribute.
    • Addes support for JS validation of "accept" attribute for file inputs.
    • Added support for JS validation via "pattern" attribute.
    • "strictHtmlValidation" option in "fieldOptions" is now set to true by default.
    • General bugfix.
  • 1.2.0
    Now written with plain JavaScript ( ES5 ) and jQuery, Minor bugfix.
  • 1.1.2
    First stable version.

Bugs

If you find a bug, please report it here