Skip to content Skip to sidebar Skip to footer

Parsley.js Date Validation In V2.x

Is it possible to use the date validation in Parsley v2.X as in the previous version? Example (v1.x): parsley-onorafterdate='#currentDate' I cannot find information in the document

Solution 1:

I believe for any validators outside of the documented core validators you will need to either copy a pre-built validator or make your own.

To add additional validators you will simply need to add the validator to a window config variable before you include parsley.

They have an example here: http://parsleyjs.org/doc/examples/customvalidator.html

If you are using requirejs I believe you would be able to create a new module and then simply require your additional validators in the requirejs define method although I haven't tested that theory yet!

As an example:

First I define my custom parsley dom attribute

  <input name="date"type="text" data-parsley-trigger="change" data-parsley-date required/>

In my script before parsley is loaded:

window.ParsleyConfig = window.ParsleyConfig || {};

 window.ParsleyConfig.validators = window.ParsleyConfig.validators || {};

 window.ParsleyConfig.validators.date = {
        fn: function (value) {
            return/^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/.test(value);
        },
        priority: 256
    };

The ParsleyConfig.validators.date matches my custom dom attribute (date) and the function returns true or false based on a regex.

In theory if you have the logic for the old parsley validation method you want to use you can just paste that inside your custom validator, but hopefully it should look pretty easy to build you own!

Post a Comment for "Parsley.js Date Validation In V2.x"