1.0.0 • Published 6 years ago

@enricoboccadifuoco/h2o-form-validator v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
6 years ago

H2o form validator

H2o form validator is a simple and tiny form validation library, 3KB minified, no dependecies.

Install

npm i @enricoboccadifuoco/h2o-form-validator

How it works

Form Validation or single fields validation

    var formValidator = validator(domFormObject);

    formValidator.on('error', function(e) {
        // log error
        console.error(e.detail.message, e.detail.el);
    });

    if (formValidator.validate()) {
        // form is correctly validate
    } else {
        // validation not successful
    }

HTML implementation

<form action="#">
    <input type="text" data-validation="alphanumeric" minlength="10" required>
    <button type="submit">Submit</button>
</form>
attributespossible valuesdefault
data-validation(see allowed validation types)none
minlengthnumber
maxlengthnumber
requiredBoolean

Events

Validation trigger the following events:

  • error => returns the following params, message and DOM element

Validation types

  • email
  • url
  • noProtocolUrl
  • alphanumeric
  • number
  • alpha

Error messages

  • email: Please provide a valid e-mail address
  • url: Please provide a valid URL
  • noProtocolUrl: empty
  • alphanumeric: Letters and numbers only, please
  • number: Numbers only, please
  • alpha: Letters only, please

Helpers

validator object return a static object called helpers. It contains static helper methods.

  • addHttp: validate a url without protocol and add http into it