admin管理员组

文章数量:1325236

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 documention about this issue.

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 documention about this issue.

Share Improve this question edited Apr 10, 2014 at 2:42 dvdgsng 1,74116 silver badges27 bronze badges asked Apr 9, 2014 at 17:37 er_benjier_benji 3054 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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/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!

本文标签: javascriptParsleyjs date validation in v2xStack Overflow