问题描述
是否可以像以前的版本一样使用Parsley v2.X中的日期验证?
Is it possible to use the date validation in Parsley v2.X as in the previous version?
示例(v1.x):parsley-onorafterdate =# currentDate
Example (v1.x): parsley-onorafterdate="#currentDate"
我在文档中找不到有关此问题的信息。
I cannot find information in the documention about this issue.
推荐答案
我相信对于所记录的核心验证器之外的任何验证器,您需要复制预先构建的验证器或自己创建验证器。
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
如果您使用的是requirejs我相信您可以创建一个新模块然后只需要在requirejs define方法中使用你的额外验证器,尽管我还没有测试过这个理论!
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!
例如:
首先我定义我的自定义欧芹dom属性
First I define my custom parsley dom attribute
<input name="date" type="text" data-parsley-trigger="change" data-parsley-date required/>
在我的脚本加载欧芹之前:
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
};
ParsleyConfig.validators.date匹配我的自定义dom属性(日期),函数返回true或false基于正则表达式。
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!
这篇关于Parsley.js在v2.x中进行日期验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!