VERSION NOTICE
This page documents Parsley 1.x versions, which is NOT the latest stable release (2.x versions are). Unless you are here on purpose, you probably want to be viewing the latest documentation.
Go to the latest: parsleyjs.org
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 parsley-API and Parsley customizable behavior. |
|
Just include the Parsley.js file (with jQuery or Zepto with data and fx_methods modules added)
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="parsley.js"></script>
<form parsley-validate>
[...]
</form>
parsley-
API, which is so nice and simple, but absolutely not W3C valid. This is a personal choice, deal with it :) But, if you really want to be W3C compliant, feel free to configure your own DOM-API namespace by overriding Parsley default configs or using data-parsley-namespace="data-parsley-"
and use data-parsley-validate
to auto bind forms. (see below)
This is the main Parsley form validation factory, where you decide if a form is validated, and set some extra options.
<form parsley-validate></form>
novalidate
attribute to form
tag.
If you want to prevent parsley from using any HTML5 validation attributes you can disable it by setting the useHtml5Constraints
flag to false in the advanced configuration options, or using parsley-use-html5-constraints="false"
in DOM.
Property | Default | Description | Demonstration | ||
---|---|---|---|---|---|
data-parsley-namespace |
parsley- | Choose the DOM-API you want to use for Parsley. | |||
parsley-validate data-parsley-validate
|
Bind Parsley validation to the form and its items with validation constraints | ||||
parsley-use-html5-constraints |
true | Bind or not supported HTML5 tags | |||
parsley-show-errors |
true | Choose to show or not Parsley error messages & success / errors classes | |||
parsley-focus
|
first |
Specify which field will be focused first on form validation errors. first , last and none allowed
|
|
||
parsley-bind |
The attribute parsley-validate must be used on a form. However, if you want to bind the validation to a DOM element other than a form, just add this attribute.
|
Once your form is validated by Parsley, add validation constraints and various other options.
<form parsley-validate>
<input type="text" name="email" parsley-type="email" />
</form>
Property | Default | Description | Demonstration |
---|---|---|---|
parsley-trigger
|
none | Specify one or many javascript events that will trigger item validation. eg: parsley-trigger="change" . To set multiple events, separate them by a space parsley-trigger="focusin focusout" .See the various events supported by jQuery. |
|
parsley-value
|
Override the value attribute of a form field. Useful for file inputs, where you're not allowed to
set a value. eg: parsley-value="uploadedfile.png"
|
||
parsley-validation-minlength
|
3 |
Specify how many characters need to be input before the the validation process fires. eg: parsley-validation-minlength="4"
|
|
parsley-error-message
|
Customize a unique global message for this field. Shown if one constraint fails.
eg:
|
||
parsley-error-container
|
Specify here the container where Parsley should set the errors.
eg:
|
||
parsley-`constraintName`-message parsley-type-`type`-message
|
Customize the error message for the field constraint. eg: parsley-minlength-message="Custom minlength message" parsley-type-email-message="Custom email message"
|
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 | parsley-minwords="6" |
Validate that a field has at least 6 words. |
Max Words | parsley-maxwords="6" |
Validate that a field has 6 words maximum. |
Range Words | parsley-rangewords="[6,10]" |
Validate that a field has between 6 and 10 words. |
Greater Than | parsley-greaterthan="#elem" |
Validate that a field's value is greater than #elem's value. |
Less Than | parsley-lessthan="#elem" |
Validate that a field's value is lower than #elem's value. |
Before date | parsley-beforedate="#elem" |
Validate that a field's date is before #elem's date. |
On or before date | parsley-onorbeforedate="#elem" |
Validate that a field's date is on or before #elem's date. |
After date | parsley-afterdate="#elem" |
Validate that a field's date is after #elem's date. |
On or after date | parsley-onorafterdate="#elem" |
Validate that a field's date is on or after #elem's date. |
In list | parsley-inlist="foo, bar, foo bar" |
Validates that a field's value is present within the value list. You can define the delimiter using parsley-inlist-delimiter="," . Delimiter defaults to "," .
|
Luhn | parsley-luhn="true" |
Validates that a fields value passes the Luhn algorithm. Validates credit card numbers, as well as some other kinds of account numbers. |
American Date | parsley-americandate="true" |
Validates that a value is a valid American Date. Allows for slash, dot and hyphen delimiters and condensed dates (e.g., M/D/YY MM.DD.YYYY MM-DD-YY). |
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 parsley-validate
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 onFormValidate . Their passed arguments are listed above in Default Plugin config. Note that if onFormValidate callback returns false , form won't be submitted, even if Parsley valid. It allows you to add another custom check on top of Parsley. Similarly, if onFieldSuccess returns false , field would be considered as invalid and form too. |
|
|
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 is a growing community of tools integrating Parsley with other frameworks. If you create such a tool, please submit a pull request.
There are plenty master tricks for Parsley out there. Please, have a look to Stackoverflow Parsley related questions for more info. If you find a real bug, then feel free to open a Github issue, with bug description and if possible a live example (jsfiddle, self hosted, pastebin..) to help us spot and fix it. Thanks.