A wrapper for https://github.com/cferdinandi/validate as they're ignoring my NPM publish requests, and using it directly from github as suggested is mad shonky.
Install
npm i --save validate-npm-wrapper
A lightweight form validation script that augments native HTML5 form validation elements and attributes, providing a better user experience and giving you more control.
When a visitor leaves a field, Validate.js immediately validates the field and displays an error if applicable. It also validates the entire form on submit, and provides support for custom onSubmit()
functions (for example, Ajax form submission).
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 Validate.js on your site.
There are two versions of Validate: the standalone version, and one that comes preloaded with a polyfill for the Validaty State API, which is only supported in newer browsers and implemented inconsistently.
If you're including your own polyfill or don't want to enable this feature for older browsers, use the standalone version. Otherwise, use the version with the polyfill.
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. Validate 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 --> <!-- Get a specific version with legacy browser support -->
2. Use HTML5 semantic input types and validation-related attributes on your form fields.
Add the required
attribute to required fields. Use type="email"
and type="url"
for email addresses and URLs, respectively. Include pattern
attributes, min
and max
, and so on to set validation criteria for your form fields. View a full list of types and attributes on MDN.
Email URL
If you're using validation patterns, you can also include a title
with a custom validation message. This will display in the error message.
Password (At least 1 uppercase character, 1 lowercase character, and 1 number)
3. Flag your form for validation.
Add the [data-validate]
attribute to any forms you want validated.
...
4. Initialize Validate.js.
In the footer of your page, after the content, initialize Validate.js. And that's it, you're done. Nice work!
ES6 Modules
Validate does not have a default export, but does support CommonJS and can be used with native ES6 module imports.
import"/path/to/validate.polyfills.min.js";
It uses a UMD pattern, and should also work in most major module bundlers and package managers.
Styling Errors
Validate.js 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 */
Working with the Source Files
If you would prefer, you can work with the development code in the src
directory using the included Gulp build system. This compiles, lints, and minifies code.
Dependencies
Make sure these are installed first.
Quick Start
- In bash/terminal/command line,
cd
into your project directory. - Run
npm install
to install required files. - When it's done installing, run one of the task runners to get going:
gulp
manually compiles files.gulp watch
automatically compiles files when changes are made and applies changes using LiveReload.
Options and Settings
Validate.js 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.
Global Settings
You can pass options and callbacks into Validate through the init()
function:
validate;
Use Validate.js events in your own scripts
You can also call Validate.js's public methods in your own scripts.
hasError()
Check if a field has a validation error.
validate;
Example
var field = document;var error = validate; if error // Do something...
showError()
Show an error message on a field.
validate;
Example 1: Write your own error
var field = document;var error = "This field is wrong, dude!";validate;
Example 2: Using hasError()
var field = document;var error = validate;validate;
removeError()
Remove the error message from a field.
/** * Remove an error message from a field * @public * @param * @param */validate;
Example
var field = document;validate;
destroy()
Destroy the current validate.init()
. Removes all errors and resets the DOM. This is called automatically during the init
function to remove any existing initializations.
validate;
Browser Compatibility
Validate.js works in all modern browsers, and (mostly) IE 10 and above.
Unfortunately, not all validation types are supported by all versions of IE and Edge consistently. For example, IE10 and IE11 will check if a form input is too long (using the maxLength
attribute), but Edge will not. And no version of IE or Edge will check if it's too short (using the minLength
attribute).
Polyfills
Use the included polyfill version of Validate (or include your own) to push support back to IE10, and add missing features to partially supported browsers.
If you also include Eli Grey's classList.js polyfill, you can push support even further, back to IE9.
License
The code is available under the MIT License.