Classy demo | HTML Code (yes, no JS !) |
---|---|
Please, note that this demo form is not a perfect example of UX-awareness. The aim here is to show a quick overview of data-API and Parsley customizable behavior. |
|
Just include the Parsley.js file (with jQuery or Zepto with data and fx_methods modules added) or use Parsley standalone (with Zepto bundled inside)
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="parsley.js"></script>
<form data-validate="parsley">
[...]
</form>
This is the main Parsley form validation factory, where you decide if a form is validated, and set some extra options.
<form data-validate="parsley"></form>
novalidate
attribute to form
tag.
Property | Default | Description | Demonstration | ||
---|---|---|---|---|---|
data-validate |
Bind Parsley validation to the form and its items with validation constraints | ||||
data-focus
|
first |
Specify which field will be focused first on form validation errors. first , last and none allowed
|
|
Once your form is validated by Parsley, add validation constraints and various other options.
<form data-validate="parsley">
<input type="text" name="email" data-type="email" />
</form>
Property | Default | Description | Demonstration |
---|---|---|---|
data-trigger
|
none | Specify one or many javascript events that will trigger item validation. eg: data-trigger="change" . To set multiple events, separate them by a space data-trigger="focusin focusout" .See the various events supported by jQuery. |
|
data-validation-minlength
|
3 |
Specify how many characters need to be input before the the validation process fires. eg: data-validation-minlength="4"
|
|
data-error-message
|
Customize a unique global message for this field. Shown if one constraint fails.
eg:
|
||
data-error-container
|
Specify here the container where Parsley should set the errors.
eg:
|
||
data-`constraintName`-message data-type-`type`-message
|
Customize the error message for the field constraint. eg: data-minlength-message="Custom minlength message" data-type-email-message="Custom email message"
|
Parsleys DOM-API is great. But not so great for complex forms, or dynamically generated forms. Learn how to master Parsley javascript style!
Warning : you must remove data-validate="parsley"
auto-binding code in your forms DOM to allow you to override the default processing and use Parsley purely from javascript.
Method | Returns | Description | Code |
---|---|---|---|
General |
|||
Override default plugin configs | Parsley uses a set of default configs that you can override by passing an object as a parameter when calling Parsley on a form or a field. Default is:
|
Add a custom validator that checks if number is a multiple of another:
|
|
Add a listener | Allows you, even after Parsleys initial form binding, to add a custom listener. Available listeners currently are: onFieldValidate , onFieldError , onFieldSuccess and onFormSubmit . Their passed arguments are listed above in Default Plugin config. |
|
|
Form |
|||
Bind Parsley to a form | Useful if your form is dynamically rendered. |
|
|
Validate a form | Boolean | Useful if you want to integrate the form validation process inside custom functions. |
|
Test if form valid | Boolean | Useful if you want to integrate the form validation process inside custom functions, without triggering error messages. |
|
Destroy Parsley | Reset error messages, error classes, stop prevent form submission and validation triggered events. |
|
|
Dynamically add an Item to Form | If an item is dynamically created, it won't be naturally validated with Parsley. Use this to attach it to existing Parsley validated form. |
|
|
Dynamically remove an Item to Form | If an item is dynamically deleted / removed from form, it will be unfortunately still present in Parsley validation process. Use this to remove it in Parsley form validation process. This field must have an id to be properly destroyed |
|
|
Field |
|||
Validate a field | Boolean | Useful if your want to integrate the field validation process inside custom functions. |
|
Add new constraint | Useful if your want to add a constraint to an existing Parsley validated field. |
|
|
Update constraint | Useful if your want to update an existing constraint for a field. |
|
|
Remove constraint | Useful if your want to remove an existing constraint to an existing Parsley validated field. |
|
More generally, in the Parsley API: All ParsleyForm and ParsleyItem functions can be publicly called using the following syntax:
$( '#parsleyFormOrFieldId' ).parsley( 'functionName', parameter );
So help yourself, read the code and play with Parsley! ;)
A good #UX goes with a good #UI. Parsley has some base classes you can use to customize things a bit ;) (and of course, rename in the configuration object)
Class | Description |
---|---|
Default classes & templates | |
.parsley-validated |
Auto added on each form item that has Parsley validation. |
.parsley-success |
Auto added on each form item that has successfully passed validation. |
.parsley-error |
Auto added on each form item that did not pass Parsley validation. |
ul.parsley-error-list |
Auto added after each form item that did not pass Parsley validation. Container for errors <li> . |
li.parsley-error |
Message displayed if constraint failed validation. |
Override them! | |
Change class names |
|
Change class handler | Add parsley-success and parsley-error to direct parent:
|
Change error container | Override the container that the By default this function does not return anything and so the For example, to have the error messages appear before the field with the error in a
|
Advanced changes | See errorsWrapper , errorElem errors properties in Parsley default options. |
Parsley is shipped with localization (i18n) for its failure messages, and with extra, advanced validators, not included in parsley by default. Learn how to use them here.
Here are Parsleys error messages in various supported languages. To use one of these, just call the desired language file. Eg: for French messages:
<script type="text/javascript" src="/i18n/messages.fr.js"></script>
<script type="text/javascript" src="parsley.js"></script>
Please, feel free to fork and contribute by adding your own translations messages in your language!
There are some extra validators, not shipped along with default Parsley, because they're more specific and less common, and there is no need to add weight to Parsley instead of calling these validators only when needed. Just call parsley.extend.js
or dist/parsley.extend.min.js
like this:
<script type="text/javascript" src="parsley.extend.fr.js"></script>
<script type="text/javascript" src="parsley.js"></script>
Please, feel free to fork and contribute by adding some useful validators!
Name | Api | Description |
---|---|---|
Min Words | data-minwords="6" |
Validate that a field has at least 6 words. |
Max Words | data-maxwords="6" |
Validate that a field has 6 words maximum. |
Range Words | data-rangewords="[6,10]" |
Validate that a field has between 6 and 10 words. |
Greater Than | data-greaterthan="#elem" |
Validate that a field's value is greater than #elem's value. |
Less Than | data-lessthan="#elem" |
Validate that a field's value is lower than #elem's value. |
Before date | data-before-date="#elem" |
Validate that a field's date is before #elem's date. |
After date | data-after-date="#elem" |
Validate that a field's date is after #elem's date. |
In list | data-inlist="foo, bar, foo bar" |
Validates that a field's value is present within the value list. You can define the delimiter using data-inlist-delimiter="," . Delimiter defaults to "," .
|
Luhn | data-luhn="true" |
Validates that a fields value passes the Luhn algorithm. Validates credit card numbers, as well as some other kinds of account numbers. |
There is a growing community of tools integrating Parsley with other frameworks. If you create such a tool, please submit a pull request.