Socket
Book a DemoInstallSign in
Socket

validator-lite

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

validator-lite

Lightweight Form Validation (no dependencies)

latest
Source
npmnpm
Version
0.2.1
Version published
Maintainers
1
Created
Source

validator-lite Build Status

Lightweight Form Validation (no dependencies)

Install

$ npm install --save validator-lite

Usage

<form id="demo">
  <div class="form-control">
    <label>Email Address</label>
    <input type="email" name="email">
  </div>
  <div class="form-control">
    <label>Password</label>
    <input id="pw" type="password" name="password1">
  </div>
  <div class="form-control">
    <label>Confirm Password</label>
    <input type="password" name="password2">
  </div>
  <button type="submit">Register</button>
</form>

<script src="validator.min.js"></script>
<script>
  var demo = document.getElementById('demo');
  var val = new Validator({
    form: demo,
    fieldClass: 'form-control',
    rules: {
      email: {
      	required: true,
      	message: 'You must enter an email',
      	test: 'email'
      },
      password1: {
      	required: true,
      	message: 'You must enter a password',
      	test: 'password'
      },
      password2: {
      	required: true,
      	test: 'equals'
      	message: 'Please confirm your password',
      	arg: function () {
          return demo.querySelector('input#pw').value;
      	}
      },
    },
    onFail: function () {
      alert('Please correct all errors!');
    },
    onPass: function (elem) {
      elem.reset();
      window.location.href = '/welcome';
    }
  });
</script>


API

Validator([options], [tests]);

Add custom tests or override included defaults.

options.form

Type: Element
Default: null

Lorem ipsum.

options.rules

Type: Object
Default: {}

Lorem ipsum.

options.errorClass

Type: String
Default: 'error'

Lorem ipsum.

options.defaultMsg

Type: String
Default: 'Required'

Lorem ipsum.

options.onFail

Type: Function
Default: noop

Lorem ipsum.

options.onPass

Type: Function
Default: noop

Lorem ipsum.

tests.email(val)

tests.password(val, arg)

tests.url(val)

tests.minLength(val, arg)

tests.maxLength(val, arg)

tests.btwLength(val, arg)

tests.equals(val, arg)

License

MIT © Luke Edwards

Keywords

browser

FAQs

Package last updated on 02 Dec 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts