Validator
This module is to validate HTML forms. Text fields, emails, phones, checkobxes etc. Check out demo.
Overview
Installation
Install validator module
npm i -S html-form-validation
or with yarn
yarn add html-form-validation
Add validator to your project
AMD
;
CommonJS
var Validator = ;
ES6
;
Inline
Also, include CSS file
Usage
Markup
Validator module needs proper HTML-markup (more in example section)
<!-- Email field --> <!-- Text field. With min and max length --> <!-- Text field. With custom error message, 'equal' codition --> <!-- Validate form button --> Validate form
Default initialization
// initialize; // initialize with options;
Initialization with webpack
// import validator; // fix jQuery conflictValidator; // use it;
Options (html)
Form fields
Option name | Possible values | Description |
---|---|---|
data-validation | required / false | Validates the field when set to true. |
data-validation-type | text / phone / email / checkbox / radio / select | Which method used to validate field. Each type has its own. |
data-validation-text | any string | Text used as error message. Otherwise validator will use its own messages for every field type. |
Field types
Type | Description | Available input types |
---|---|---|
text | Validates text field. Input can have additional attribute data-validation-condition with available length and equal values. If set to length - Validator will look for data-min-length, data-max-length or data-length attributes. If set to equal - Validator will look for data-equal attribute. Then validator will match value with values from attributes. | input / textarea |
phone | Under development. No additional options are available. | input |
Validates email field. No additional options are available. | input | |
checkbox | At least one checkbox in field should be checked and visible. No additional options are available. | input[type="checkbox"] |
radio | At lease one radio should be selected. No additional options are available. | input[type="radio"] |
select | Checks for selected option. Its value should not equal 0 or false. No additional options are available. | select |
Options (js)
ajax
Type: Object
Default: {}
AJAX options. If set - request will be performed after validation (if form is valid).
lang
Type: String
Default: en
Error text language (en/ru).
removeErrorOnFocusOut
Type: Boolean
Default: false
When true, remove fields incorrect state when clicked outside the form.
fieldsSelector
Type: String
Default: '.form-input'
Form fields selector string.
beforeValidation
Type: Function
Default: null
Parameter: validator
Example:
;
Callback performed before form validation.
afterValidation
Type: Function
Default: null
Parameter: validator
Callback performed after form validation.
onValid
Type: Function
Default: null
Parameter: validator
Callback performed after validation, if form is valid (before AJAX).
Methods
Instance method
// initialize and get access to validator's instance// (if inited on multiple jQuery objects returns an array of instances)var validatorInstance = ; // run form validationvalidatorInstance; // reset formvalidatorInstance; // get serialized datavar formData = validatorInstance; // unbind validation from buttonvalidatorInstance;
Static methods
/** * Expose validator module as jquery plugin. * Use before initialazing validator. * (fixes jquery conflict when using webpack's "import") * * @static * @param */ Validator;
TODO
- Alphabet / numeric characters validation
- Correct phone number validation
- Simplify data-attr logic
- Refactor errors text / languages
- Add 'afterChange' validation
Requirement
Versioning
Current version is 0.2.2