Jquery plugin that guards your forms.
- As simple and fast as it could be.
- Allows you to validate single fields, custom sections of form (i.e.fieldsets) and whole form.
- Multiform validation
- Provides public methods, to check validation inside your own code.
- Behaves well with form nodes styling scripts.
- Allows you to pass custom error messages and validation rules.
Download and wire the plugin:
bower install --save-dev jquery-guard
Make a plugin instance:
var guard = $(formSelector).guard(options, errors, rules);
for example:
var guard = $(#form).guard({sections:'fieldset', onSubmit: function(e){this.submit;}}, errors, rules);
Guard chosen fields with a rules using data-guard="rulename"
:
<input type="text" data-guard="required">
Suppose that you have a long form with 4 steps / parts / sections. Suppose you want to validate a section, before loading the next one. Just use the check
public method:
$('.goToNextSection').click(function(e){ e.preventDefault(); if (guard.check($(this).parents('fieldset').index()) $.get('step2.html'); });
You can pass in custom or translated error messages. Simply pass in an object structures as the on in file `src/lang/pl.js'
parentClass
:string
|| default :form-group
|| class of the parent containing the field, the element will get theguard-invalid
class and an error node appenedsections
:string
orfalse
|| default :false
|| if the form contains sections, that needs to be validated separatly (i.e. form steps) then pass the DOM selectorlive
:boolean
|| default :true
|| whether the form should validate on go or during the submit event- callbacks (see below)
- required
- number
- maxLength{x}
- minLength{x}
- minValue{x}
- maxValue{x}
You can also pass in custom rules. See src/lang/pl.js
for an example.
check(form, section)
: the form is a jquery selector and thesection
is anint
and represents the section index number : returnsboolean
|| check if the section is valid (there are no invalid fields). If the section argument is empty, then it will check the whole form.validate(field)
: thefield
is a DOM selector : returnboolean
|| check if the particular field is valid
Every callback function has the form set as this
value and event
passed to it as an argument.
onSubmit(e)
: called during submit event, if the form is valid. If not specified, the form will normally submit.
1.2.0
- Mocha Tests