Bouncer.js
A lightweight form validation script that augments native HTML5 form validation elements and attributes.
Getting Started | Form Validation Attributes | Error Styling | Error Types | Custom Validations | Error Location | API | Browser Compatibility | License |
Features:
- Fields validate on blur (as the user moves out of them), which data shows is the best time for cognitive load.
- The entire form is validated on submit.
- Fields with errors are revalidated as the user types. Errors are removed the instant the field is valid.
- Supports custom validation patterns and error messages.
Vanilla JS Pocket Guides or join the Vanilla JS Academy and level-up as a web developer. 🚀
Want to learn how to write your own vanilla JS plugins? Check out myGetting Started
Compiled and production-ready code can be found in the dist
directory. The src
directory contains development code.
1. Include Bouncer on your site.
There are two versions of Bouncer: the standalone version, and one that comes preloaded with polyfills for closest()
, matches()
, classList
, and CustomEvent()
, which are only supported in newer browsers.
If you're including your own polyfills or don't want to enable this feature for older browsers, use the standalone version. Otherwise, use the version with polyfills.
Direct Download
You can download the files directly from GitHub.
CDN
You can also use the jsDelivr CDN. I recommend linking to a specific version number or version range to prevent major updates from breaking your site. Smooth Scroll uses semantic versioning.
<!-- Always get the latest version --><!-- Not recommended for production sites! --> <!-- Get minor updates and patch fixes within a major version --> <!-- Get patch fixes within a minor version --> <!-- Get a specific version -->
NPM
You can also use NPM (or your favorite package manager).
npm install formbouncerjs
2. Add browser-native form validation attributes to your markup.
No special markup needed—just browser-native validation attributes (like required
) and input types (like email
or number
).
Your email address Submit
3. Initialize Bouncer.
In the footer of your page, after the content, initialize Bouncer by passing in a selector for the forms that should be validated.
If the form has errors, submission will get blocked until they're corrected. Otherwise, it will submit as normal.
And that's it, you're done. Nice work!
Form Validation Attributes
Modern browsers provide built-in form validation.
Bouncer hooks into those native attributes, suppressing the native validation and running its own. If Bouncer fails to load or run, the browser-native validation will run in its place.
Required fields
Add the required
attribute to any field that must be filled out or selected.
Special Input Types
You can use special type
attribute values to indicate specific types of data that should be captured.
<!-- Must be a valid email address --> <!-- Must be a valid URL --> <!-- Must be a number --> <!-- Must be a date in YYYY-MM-DD format (many browsers include a native date picker for this) --> <!-- Must be a time in 24-hour format (many browsers include a native date picker for this) --> <!-- Must be a month/year in YYYY-MM format (many browsers include a native date picker for this) --> <!-- Must be a color in #rrggbb format (many browsers include a native color picker for this) -->
Min and Max Values
For numbers that should not go below or exceed a certain value, you can use the min
and max
attributes, respectively.
<!-- Cannot exceed 72 --> <!-- Cannot be below 37 --> <!-- They can also be combined -->
Min and Max Length
For inputs that should not be shorter or longer than a certain number of characters, you can use the minlength
and maxlength
attributes, respectively.
<!-- Cannot be shorter than 12 characters --> <!-- Cannot be longer than 24 characters --> <!-- They can also be combined -->
Custom Validation Patterns
You can use your own validation pattern for a field with the pattern
attribute. This uses regular expressions.
<!-- Phone number be in 555-555-5555 format -->
Custom Pattern Mismatch Error Messages
Show custom errors for pattern mismatches by adding the [data-bouncer-message]
attribute to the field.
<!-- Phone number be in 555-555-5555 format -->
Error Styling
Bouncer does not come pre-packaged with any styles for fields with errors or error messages. Use the .error
class to style fields, and the .error-message
class to style error messages.
Need a starting point? Here's some really lightweight CSS you can use.
/** * Form Validation Errors */
Error Types
Bouncer captures four different types of field errors:
missingValue
errors occur when arequired
field has no value (or for checkboxes and radio buttons, nothing is selected).patternMismatch
errors occur when a value doesn't match the expected pattern for a particular input type, or a pattern provided by thepattern
attribute.outOfRange
errors occur when a number is above or below themin
ormax
values.wrongLength
errors occur when the input is longer or shorter than theminlength
andmaxlength
values.
The patterns and messages associated with these types of errors can be customized.
Custom Validation Types
You can add custom validation types to Bouncer beyond the four standard validations.
You can see this feature in action with the Confirm Password field on the demo page, and view examples of custom validations in the Cookbook (coming soon).
Adding custom validations
Pass in a customValidations
object as an option when instantiating a new Bouncer instance. Each property in the object is a new validation type. Each value should be a function that accepts two arguments: the field being validated and the settings for the current instantiation.
The function should check if the field has an error. Return true
if there's an error, and false
when there's not.
var validate = 'form' customValidations: { // Return false because there is NO error if fieldvalue === 'hello' return false; // Return true when there is return true; } ;
Creating custom validation error messages
Add an error message for the custom validation by including the messages
object in your options.
The key should be the same as your custom validation. It's value can be a string, or a function that returns a string. Message functions can accept two arguments: the field being validated and the settings for the current instantiation.
var validate = 'form' customValidations: { // Return false because there is NO error if fieldvalue === 'hello' return false; // Return true when there is return true; } messages: // As a string isHello: 'This field should have a value of "hello"' // As a function { return 'This field should have a value of "hello"'; } ;
Error Message Location
By default, bouncer will render error messages after the invalid field (or the label for it, if the field is a radio
or checkbox
).
You can optionally render error messages before the field by setting the messageAfterField
option to false
.
var validate = 'form' messageAfterField: false;
You can also assign a custom location for an error message by including the [data-bouncer-target]
attribute on a field. Use a selector for where the message should go as its value.
Your Email AddressWhy do we need this? We'll use your email address to send you account information.
API
Bouncer includes smart defaults and works right out of the box.
But if you want to customize things, it also has a robust API that provides multiple ways for you to adjust the default options and settings.
Options and Settings
You can customize validation patterns, error messages, and more by passing and options object into Bouncer during instantiation.
var validate = 'form' // Classes & IDs fieldClass: 'error' // Applied to fields with errors errorClass: 'error-message' // Applied to the error message for invalid fields fieldPrefix: 'bouncer-field_' // If a field doesn't have a name or ID, one is generated with this prefix errorPrefix: 'bouncer-error_' // Prefix used for error message IDs // Patterns // Validation patterns for specific input types patterns: email: /^*\x40*+$/ url: /^???$/ number: /[-+]?[0-9]*[.,]?[0-9]+/ color: /^#?$/ date: /[0-9]{2}-/ time: // month: /[0-9]{2}-/ // Message Settings messageAfterField: true // If true, displays error message below field. If false, displays it above. messageCustom: 'data-bouncer-message' // The data attribute to use for customer error messages messageTarget: 'data-bouncer-target' // The data attribute to pass in a custom selector for the field error location // Error messages by error type messages: missingValue: checkbox: 'This field is required.' radio: 'Please select a value.' select: 'Please select a value.' 'select-multiple': 'Please select at least one value.' default: 'Please fill out this field.' patternMismatch: email: 'Please enter a valid email address.' url: 'Please enter a URL.' number: 'Please enter a number' color: 'Please match the following format: #rrggbb' date: 'Please use the YYYY-MM-DD format' time: 'Please use the 24-hour time format. Ex. 23:00' month: 'Please use the YYYY-MM format' default: 'Please match the requested format.' outOfRange: over: 'Please select a value that is no more than {max}.' under: 'Please select a value that is no less than {min}.' wrongLength: over: 'Please shorten this text to no more than {maxLength} characters. You are currently using {length} characters.' under: 'Please lengthen this text to {minLength} characters or more. You are currently using {length} characters.' // Form Submission disableSubmit: false // If true, native form submission is suppressed even when form validates // Custom Events emitEvents: true // If true, emits custom events ;
Custom Events
Bouncer emits five custom events:
bouncerShowError
is emitted on a field when an error is displayed for it.bouncerRemoveError
is emitted on a field when an error is removed from it.bouncerFormValid
is emitted on a form is successfully validated.bouncerFormInvalid
is emitted on a form that fails validation.bouncerInitialized
is emitted when bouncer initializes.bouncerDestroy
is emitted when bouncer is destroyed.
You can listen for these events with addEventListener
. All five events bubble up the DOM. The event.target
is the field or form (or document, when initializing and destroying).
// Detect show error eventsdocument; // Detect a successful form validationdocument;
The event.detail
object holds event-specific information:
- On the
bouncerShowError
event, it has the specific errors for the field. - On the
bouncerInitialized
andbouncerDestroyed
events , it contains thesettings
for the instantiation. - On the
bouncerFormInvalid
event, it includes all of the fields with errors underevent.detail
.
// Detect show error eventsdocument;
Using Bouncer methods in your own scripts
Bouncer exposes a few public methods that you can use in your own scripts.
validate()
Validate a field. Pass in the field as an argument. Returns an object with validity data.
// Get a fieldvar field = document; // Validate the fieldvar bouncer = ;var isValid = bouncer; // Returns an objectisValid = // If true, field is valid valid: false // The specific errors errors: missingValue: true patternMismatch: true outOfRange: true wrongLength: true ; // You can also pass in custom optionsbouncer;
validateAll()
Validate all fields in a form or fieldset. Pass in the section as an argument. Returns an array of fields with errors.
// Get a fieldsetvar fieldset = document; // Validate the fieldvar bouncer = ;var areValid = bouncer;
destroy()
Destroys an instantiated Bouncer instance. Removes any errors from the form and turns validation back over to the browser-native APIs.
// An bouncer instancevar bouncer = 'form'; // Destroy itbouncer;
Browser Compatibility
Bouncer works in all modern browsers, and IE 9 and above.
Bouncer is built with modern JavaScript APIs, and uses progressive enhancement. If the JavaScript file fails to load, browser-native form validation runs instead
Polyfills
Support back to IE9 requires polyfills for closest()
, matches()
, classList
, and CustomEvent()
. Without them, support starts with Edge.
Use the included polyfills version of Bouncer, or include your own.
License
The code is available under the MIT License.