New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

dominar

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

dominar

Lighweight and highly configurable bootstrap validator

  • 0.0.2
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

Dominar

Build Status

Lightweight and highly configurable boostrap validator built on-top of validator.js.

Usage

var validator = new Dominar($('form'), {
   email: {
      rules: 'required|email'
   },
   username: {
      rules: 'required|min:3|max:10',
      triggers: ['keyup', 'change', 'focusout'],
      delay: 300
   }
});

Note: See below for all possible options. Only rules is required.

Demo

http://garygreen.github.io/dominar/

Installation

bower install dominar --save

The main file to include is dist/dominar-standalone.js. If you already have validator.js installed then just simply use dist/dominar.js


HTML Structure

By default it is assumed your element is contained inside a .form-group

<div class="form-group">
   <label>Username</label>
   <input type="text" name="username"/>
</div>

You can change this by supplying the container option e.g. container: 'td'

Available validation rules

  • accepted
  • alpha
  • alpha_dash
  • alpha_num
  • confirmed
  • digits:value
  • different:attribute
  • email
  • in:foo,bar,..
  • integer
  • max:value
  • not_in:foo,bar,..
  • numeric
  • required
  • same:attribute
  • size:value
  • url
  • regex:pattern

See here for more rules & usage details.

Custom validation rule

Add a custom validation rule:

Dominar.register('uppercase', function(value) {
   return value.toUpperCase() === value;
}, 'The :attribute must only be uppercase.');

Asynchronous / Ajax validation rules

Use remoteRule which takes a deferred object (like $.ajax and $.get returns).

var dominar = new Dominar($('form'), {
   username: {
      rules: 'required',
      remoteRule: function(desiredUsername) {
         return $.get('/api/check-username', { username: desiredUsername });
      }
   }
});

On your server return HTTP status code 200 if validation passes or if not, a 4xx json response with the error message:

{"message": "Username already taken."}

Custom error message

By default error messages are automatically generated for you. If you would like to customise, use the customMessages option to specify a custom error message for the rules.

username: {
   rules: 'required|min:5',
   customMessages: {
      required: 'Whoops, :attribute field is required!',
      min: 'This needs more characters :('
   }
}

Customising placement of error messages

Just manually add anywhere inside your .form-group a .help-block and dominar will automatically detect and use.

<div class="form-group">
   <div class="help-block"></div>
   <input type="text" name="username"/>
</div>

Note: by default dominar will automatically add errors message straight after the input element.

Changing default options

If you want to change the default options you can simply overwrite on the prototype like in the below example. This is useful if you want to always use e.g. fontawesome icons instead of glyphicons. Of course these are just defaults and can still be customised on a per-field level.

Dominar.prototype.defaults = $.extend({}, Dominar.prototype.defaults, {
   feedbackIcons: {
      error: '<i class="fa fa-remove"></i>',
      success: '<i class="fa fa-check"></i>'
   }
});

Options

OptionTypeDescription
rulesstringSet of rules seperated by pipe
triggersarray/falseDetermines when validation will be triggered on element. Set to false to turn off automatic triggering.
delayinteger/falseDelay in triggering validation when typing in a field. Set to false to always trigger validation as soon as possible.
delayTriggersarrayDetermines when validation will be triggered as a delay on element.
containerstringThe selector for the element container
messagebooleanWhether to display error messages or not
customMessagesobjectSet custom error messages for the rules
feedbackbooleanWhether to display feedback icon or not
feedbackIconsobjectConfigure the success and error feedback icons
remoteRulefunctionAsynchronous rule to run

Events

Dominar will fire various events on the form. You can listen for the events like:

$('#my-form').bind('dominar.init-field', function(event) {
   var dominar = event.dominar;
   var field = event.dominarField; // The DominarField which has been initialized
});

The submit event allows you to prevent the validation from occuring by preventing the default action:

$('#my-form').bind('dominar.submit', function(event) {
   event.preventDefault(); // Prevent form from being validated
});
NamePreventableDescription
dominar.init-fieldNoWhen a DominarField has been initialized (useful for adding additional event listeners to the input element etc)
dominar.submitYesWhen form is about to be submitted and before validation check has been run.
dominar.submit-passedYesWhen form passed validation and is about to be submitted.
dominar.submit-failedNoWhen failed validation check when form was attempted to be submitted.

FAQs

Package last updated on 05 Sep 2015

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc